|
| 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 |
0 commit comments