Skip to content

Commit 68664b6

Browse files
committed
Added squarepine_location.
1 parent c9d6d58 commit 68664b6

File tree

6 files changed

+239
-0
lines changed

6 files changed

+239
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
GPSLocation::GPSLocation(float latitude, float longitude, double a) :
2+
GPSLocation({ latitude, longitude }, a)
3+
{
4+
}
5+
6+
GPSLocation::GPSLocation (Point<float> coords, double a) :
7+
coordinates (coords),
8+
altitude (a)
9+
{
10+
}
11+
12+
GPSLocation::GPSLocation (const GPSLocation& other) :
13+
coordinates (other.coordinates),
14+
altitude (other.altitude)
15+
{
16+
}
17+
18+
GPSLocation& GPSLocation::operator= (const GPSLocation& other)
19+
{
20+
coordinates = other.coordinates;
21+
altitude = other.altitude;
22+
return *this;
23+
}
24+
25+
GPSLocation& GPSLocation::operator= (GPSLocation&& other)
26+
{
27+
coordinates = other.coordinates;
28+
altitude = other.altitude;
29+
return *this;
30+
}
31+
32+
bool GPSLocation::operator== (const GPSLocation& other) const
33+
{
34+
return coordinates == other.coordinates
35+
&& altitude == other.altitude;
36+
}
37+
38+
bool GPSLocation::operator!= (const GPSLocation& other) const
39+
{
40+
return ! operator== (other);
41+
}
42+
43+
bool GPSLocation::isNull() const { return coordinates.isOrigin(); }
44+
45+
#if JUCE_IOS || JUCE_MAC
46+
47+
/**
48+
Please see GPSLocation.mm for the Apple platform implementation!
49+
*/
50+
51+
#elif JUCE_ANDROID
52+
53+
bool GPSLocation::isLocationAvailable()
54+
{
55+
if (auto* jniEnv = GetJNIEnv())
56+
{
57+
static jmethodID _isLocationAvailable = nullptr;
58+
if (_isLocationAvailable == nullptr)
59+
_isLocationAvailable = jniEnv->GetMethodID (GetJNIActivityClass(), "_isLocationAvailable", "()Z");
60+
61+
return jniEnv->CallBooleanMethod (cachedActivity, _isLocationAvailable);
62+
}
63+
64+
return false;
65+
}
66+
67+
std::optional<GPSLocation> GPSLocation::getCurrentLocation()
68+
{
69+
if (auto* jniEnv = GetJNIEnv())
70+
{
71+
static jmethodID _getGPSLocation = nullptr;
72+
if (_getGPSLocation == nullptr)
73+
_getGPSLocation = jniEnv->GetMethodID (GetJNIActivityClass(), "_getGPSLocation", "()[D");
74+
75+
auto javaGPSCurrentLocationResult = (jdoubleArray) jniEnv->CallObjectMethod (cachedActivity, _getGPSLocation);
76+
if (javaGPSCurrentLocationResult == nullptr)
77+
return {}; // If this is reached, the GPS permission was probably disabled but it's hard to say exactly...
78+
79+
auto numItems = jniEnv->GetArrayLength (javaGPSCurrentLocationResult);
80+
std::vector<double> input (numItems);
81+
jniEnv->GetDoubleArrayRegion (javaGPSCurrentLocationResult, 0, numItems, &input[0]);
82+
83+
GPSLocation result;
84+
decltype (numItems) index = 0;
85+
if (index < numItems) result.coordinates.x = (float) input[index++];
86+
if (index < numItems) result.coordinates.y = (float) input[index++];
87+
if (index < numItems) result.altitude = (double) input[index++];
88+
89+
return result;
90+
}
91+
92+
return {};
93+
}
94+
95+
#else
96+
97+
bool GPSLocation::isLocationAvailable() { return false; }
98+
std::optional<GPSLocation> GPSLocation::getCurrentLocation() { return {}; }
99+
100+
#endif
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/** Represents a GPS location that provides the coordinates and altitude.
2+
3+
Call isLocationAvailable to find out if the system can provide you a location,
4+
and then call getCurrentLocation to get the system's location.
5+
*/
6+
class GPSLocation final
7+
{
8+
public:
9+
/** Represents a null GPS location.
10+
11+
@see isNull
12+
*/
13+
GPSLocation() = default;
14+
15+
/** Creates a GPS location based on the provided coordinates and altitude. */
16+
GPSLocation (float latitude, float longitude, double altitude = 0.0);
17+
18+
/** Creates a GPS location based on the provided coordinates and altitude. */
19+
GPSLocation (Point<float> coordinates, double altitude = 0.0);
20+
21+
/** Copy constructor. */
22+
GPSLocation (const GPSLocation&) = default;
23+
24+
/** Move constructor. */
25+
GPSLocation (GPSLocation&&) = default;
26+
27+
/** Destructor. */
28+
~GPSLocation() = default;
29+
30+
/** @returns true if the location can be retrieved.
31+
32+
This can return false if the platform doesn't have location support
33+
or has location explicitly disabled.
34+
*/
35+
static bool isLocationAvailable();
36+
37+
/** @returns the current location of the platform's GPS.
38+
The instance returned might be null if this feature is not available
39+
due to platform limitations or lacking platform permissions.
40+
*/
41+
static std::optional<GPSLocation> getCurrentLocation();
42+
43+
/** @returns true if the location is null (latitude: 0.0, longitude: 0.0).
44+
45+
The vertical axis is irrespective of the location and so the
46+
altitude is not considered part of the null-ness.
47+
48+
Technically this location is valid, but may indicate an issue outside of the
49+
underlying platform APIs, such as the GPS being unable to get a fix on the location:
50+
@see https://en.wikipedia.org/wiki/Null_Island
51+
*/
52+
bool isNull() const;
53+
54+
GPSLocation& operator= (const GPSLocation&);
55+
GPSLocation& operator= (GPSLocation&&);
56+
bool operator== (const GPSLocation&) const;
57+
bool operator!= (const GPSLocation&) const;
58+
59+
Point<float> coordinates; //< The geographic location, where x is the latitude and y is the longitude.
60+
double altitude = 0.0; //< The altitude. Though typically this is in meters, it may be platform dependant.
61+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
bool GPSLocation::isLocationAvailable()
2+
{
3+
return [CLLocationManager locationServicesEnabled]
4+
&& [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied
5+
&& [CLLocationManager authorizationStatus] != kCLAuthorizationStatusRestricted;
6+
}
7+
8+
std::optional<GPSLocation> GPSLocation::getCurrentLocation()
9+
{
10+
if (auto locationManager = [app getLocationManager])
11+
{
12+
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)
13+
{
14+
[locationManager requestWhenInUseAuthorization];
15+
}
16+
17+
if (isLocationAvailable())
18+
{
19+
[locationManager requestLocation];
20+
21+
return
22+
{
23+
static_cast<float> (locationManager.location.coordinate.latitude),
24+
static_cast<float> (locationManager.location.coordinate.longitude),
25+
static_cast<double> (locationManager.location.altitude)
26+
};
27+
}
28+
}
29+
30+
return {};
31+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "squarepine_location.h"
2+
3+
//==============================================================================
4+
namespace sp
5+
{
6+
//==============================================================================
7+
#include "location/GPSLocation.cpp"
8+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef SQUAREPINE_LOCATION_H
2+
#define SQUAREPINE_LOCATION_H
3+
4+
/** BEGIN_JUCE_MODULE_DECLARATION
5+
6+
ID: squarepine_location
7+
vendor: SquarePine
8+
version: 1.7.0
9+
name: SquarePine Location
10+
description: A JUCE module that provides location services and utilities.
11+
website: https://www.squarepine.io
12+
license: GPLv3
13+
minimumCppStandard: 20
14+
dependencies: squarepine_core
15+
OSXFrameworks: CoreLocation
16+
iOSFrameworks: CoreLocation
17+
18+
END_JUCE_MODULE_DECLARATION
19+
*/
20+
21+
//==============================================================================
22+
#include <squarepine_core/squarepine_core.h>
23+
24+
//==============================================================================
25+
namespace sp
26+
{
27+
using namespace juce;
28+
29+
#include "location/GPSLocation.h"
30+
}
31+
32+
#endif // SQUAREPINE_LOCATION_H
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#undef JUCE_CORE_INCLUDE_OBJC_HELPERS
2+
#define JUCE_CORE_INCLUDE_OBJC_HELPERS 1
3+
4+
#import <CoreLocation/CoreLocation.h>
5+
6+
#include "squarepine_core.cpp"
7+
#include "location/GPSLocation.mm"

0 commit comments

Comments
 (0)