Skip to content

Commit

Permalink
update 3.5.0
Browse files Browse the repository at this point in the history
- remove cordova-plugin-add-swift-support
- (iOS) request location authorization if not set using `CLLocationManager`'s `requestWhenInUseAuthorization` function
- (Android) add `addressLines` for android [Docs](https://developer.android.com/reference/android/location/Address#getAddressLine(int))
  • Loading branch information
sebastianbaar committed Jan 12, 2023
1 parent f135b09 commit bb83264
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 21 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 3.5.0 (2023-01-12)
- remove cordova-plugin-add-swift-support
- (iOS) request location authorization if not set using `CLLocationManager`'s `requestWhenInUseAuthorization` function
- (Android) add `addressLines` for android [Docs](https://developer.android.com/reference/android/location/Address#getAddressLine(int))

# 3.4.1 (2020-04-29)
- add cordova-plugin-add-swift-support to fix Swift version errors (closes #57)

Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Call `nativegeocoder.reverseGeocode()` to transform a latitude and longitude int

## Ionic

This plugin is also available for **[Ionic Native](https://ionicframework.com/docs/native/native-geocoder/) & [Capacitor](https://ionicframework.com/docs/native/native-geocoder/)**.
This plugin is also available for **Ionic [awesome-cordova-plugins](https://danielsogl.gitbook.io/awesome-cordova-plugins/native-geocoder/) & [Capacitor](https://capacitorjs.com)**.

## Installation
```
Expand Down Expand Up @@ -66,6 +66,8 @@ Conforms to [Apple's](https://developer.apple.com/documentation/corelocation/clp
| `thoroughfare` | `String` |
| `subThoroughfare` | `String` |
| `areasOfInterest` | `Array<String>` |
| `addressLines` (Android only) | `Array<String>` |


### Example
```js
Expand Down Expand Up @@ -117,6 +119,7 @@ Conforms to [Apple's](https://developer.apple.com/documentation/corelocation/clp
| `thoroughfare` | `String` |
| `subThoroughfare` | `String` |
| `areasOfInterest` | `Array<String>` |
| `addressLines` (Android only) | `Array<String>` |

### Example
```js
Expand All @@ -137,16 +140,13 @@ function failure(err) {
### Android

#### Geocoder not working
Errors like `Geocoder is not present on this device/emulator.`, `Geocoder [...] Error` or `Geocoder Service not available`.
Errors like `Geocoder:getFromLocationName Error: grpc failed`, `Geocoder is not present on this device/emulator.`, `Geocoder [...] Error` or `Geocoder Service not available`.

**Why?**:
The plugin checks for `Geocoder.isPresent()` ([Android docs](https://developer.android.com/reference/android/location/Geocoder.html#isPresent())). One reason for Geocoder not working on a device could be that you are running your app on an emulator or on a device without Google Play Services. But Geocoder API "[...] requires a backend service that is not included in the core android framework".

**Any workaround?**:
Yes. Query [Google Maps Geocoding API](https://developers.google.com/maps/documentation/geocoding/start) as a backup if Geocoder is not present.

**Can the plugin handle this?**:
No. I decided not to implement Google Maps Geocoding API as a backup because you have to add an [API key and more](https://developers.google.com/maps/documentation/geocoding/usage-and-billing). I want this plugin to remain as small and lightweight as possible.
Yes, install Google Play services. Open `SDK Tools` > Tab `SDK Tools` > install `Google Play services`.

### iOS
...
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-nativegeocoder",
"version": "3.4.1",
"version": "3.5.0",
"description": "Cordova plugin for native forward and reverse geocoding",
"cordova": {
"id": "cordova-plugin-nativegeocoder",
Expand Down
4 changes: 1 addition & 3 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<plugin id="cordova-plugin-nativegeocoder" version="3.4.1" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<plugin id="cordova-plugin-nativegeocoder" version="3.5.0" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<name>NativeGeocoder</name>
<description>Cordova plugin for native forward and reverse geocoding</description>
<license>MIT</license>
Expand Down Expand Up @@ -34,7 +34,6 @@
<param name="ios-package" value="NativeGeocoder" />
</feature>
</config-file>
<header-file src="src/ios/NativeGeocoder-Bridging-Header.h" />

<source-file src="src/ios/NativeGeocoder.swift" />
<framework src="CoreLocation.framework" />
Expand All @@ -44,5 +43,4 @@
</config-file>
</platform>

<dependency id="cordova-plugin-add-swift-support" version="2.0.2"/>
</plugin>
16 changes: 15 additions & 1 deletion src/android/NativeGeocoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ private void reverseGeocode(double latitude, double longitude, JSONObject option
placemark.put("thoroughfare", address.getThoroughfare() != null ? address.getThoroughfare() : "");
placemark.put("subThoroughfare", address.getSubThoroughfare() != null ? address.getSubThoroughfare() : "");
placemark.put("areasOfInterest", address.getFeatureName() != null ? new JSONArray(new String[]{ address.getFeatureName()} ) : new JSONArray());

placemark.put("addressLines", this.getAddressLines(address));

resultObj.put(placemark);
}

Expand Down Expand Up @@ -182,6 +183,7 @@ private void forwardGeocode(String addressString, JSONObject options, CallbackCo
placemark.put("thoroughfare", address.getThoroughfare() != null ? address.getThoroughfare() : "");
placemark.put("subThoroughfare", address.getSubThoroughfare() != null ? address.getSubThoroughfare() : "");
placemark.put("areasOfInterest", address.getFeatureName() != null ? new JSONArray(new String[]{ address.getFeatureName() }) : new JSONArray());
placemark.put("addressLines", this.getAddressLines(address));

resultObj.put(placemark);
}
Expand Down Expand Up @@ -213,6 +215,18 @@ private void forwardGeocode(String addressString, JSONObject options, CallbackCo
}
}

/**
* Get address lines array
* @return boolean
*/
private JSONArray getAddressLines(Address address) {
JSONArray jsonArray = new JSONArray();
for (int n = 0; n <= address.getMaxAddressLineIndex(); n++) {
jsonArray.put(address.getAddressLine(n));
}
return jsonArray;
}

/**
* Get network connection
* @return boolean
Expand Down
1 change: 0 additions & 1 deletion src/ios/NativeGeocoder-Bridging-Header.h

This file was deleted.

23 changes: 14 additions & 9 deletions src/ios/NativeGeocoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,23 @@ struct NativeGeocoderOptions: Decodable {
var maxResults: Int = 1
}

@objc(NativeGeocoder) class NativeGeocoder : CDVPlugin {
@objc(NativeGeocoder) class NativeGeocoder: CDVPlugin {
private lazy var locationManager = CLLocationManager()
private lazy var geocoder = CLGeocoder()
typealias ReverseGeocodeCompletionHandler = ([NativeGeocoderResult]?, NativeGeocoderError?) -> Void
typealias ForwardGeocodeCompletionHandler = ([NativeGeocoderResult]?, NativeGeocoderError?) -> Void
private static let MAX_RESULTS_COUNT = 5

// MARK: - REVERSE GEOCODE
@objc(reverseGeocode:) func reverseGeocode(_ command: CDVInvokedUrlCommand) {
locationManager.requestWhenInUseAuthorization()

var pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR)

if let latitude = command.arguments[0] as? Double,
let longitude = command.arguments[1] as? Double {

if (CLGeocoder().isGeocoding) {
if (geocoder.isGeocoding) {
pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: "Geocoder is busy. Please try again later.")
self.commandDelegate!.send(pluginResult, callbackId: command.callbackId)
return
Expand Down Expand Up @@ -86,14 +90,14 @@ struct NativeGeocoderOptions: Decodable {
locale = Locale.init(identifier: "en_US")
}

CLGeocoder().reverseGeocodeLocation(location, preferredLocale: locale, completionHandler: { [weak self] (placemarks, error) in
geocoder.reverseGeocodeLocation(location, preferredLocale: locale, completionHandler: { [weak self] (placemarks, error) in
self?.createReverseGeocodeResult(placemarks, error, maxResults: geocoderOptions.maxResults, completionHandler: { (resultObj, error) in
completionHandler(resultObj, error)
})
})
} else {
// fallback for < iOS 11
CLGeocoder().reverseGeocodeLocation(location, completionHandler: { [weak self] (placemarks, error) in
geocoder.reverseGeocodeLocation(location, completionHandler: { [weak self] (placemarks, error) in
self?.createReverseGeocodeResult(placemarks, error, maxResults: geocoderOptions.maxResults, completionHandler: { (resultObj, error) in
completionHandler(resultObj, error)
})
Expand Down Expand Up @@ -148,11 +152,13 @@ struct NativeGeocoderOptions: Decodable {

// MARK: - FORWARD GEOCODE
@objc(forwardGeocode:)func forwardGeocode(_ command: CDVInvokedUrlCommand) {
locationManager.requestWhenInUseAuthorization()

var pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR)

if let address = command.arguments[0] as? String {

if (CLGeocoder().isGeocoding) {
if (geocoder.isGeocoding) {
pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: "Geocoder is busy. Please try again later.")
self.commandDelegate!.send(pluginResult, callbackId: command.callbackId)
return
Expand Down Expand Up @@ -200,14 +206,14 @@ struct NativeGeocoderOptions: Decodable {
locale = Locale.init(identifier: "en_US")
}

CLGeocoder().geocodeAddressString(address, in: nil, preferredLocale: locale, completionHandler: { [weak self] (placemarks, error) in
geocoder.geocodeAddressString(address, in: nil, preferredLocale: locale, completionHandler: { [weak self] (placemarks, error) in
self?.createForwardGeocodeResult(placemarks, error, maxResults: geocoderOptions.maxResults, completionHandler: { (resultObj, error) in
completionHandler(resultObj, error)
})
})
} else {
// fallback for < iOS 11
CLGeocoder().geocodeAddressString(address, completionHandler: { [weak self] (placemarks, error) in
geocoder.geocodeAddressString(address, completionHandler: { [weak self] (placemarks, error) in
self?.createForwardGeocodeResult(placemarks, error, maxResults: geocoderOptions.maxResults, completionHandler: { (resultObj, error) in
completionHandler(resultObj, error)
})
Expand Down Expand Up @@ -271,5 +277,4 @@ struct NativeGeocoderOptions: Decodable {
}
return geocoderOptions
}

}

0 comments on commit bb83264

Please sign in to comment.