Skip to content

Commit

Permalink
update plugin to v3.2.0
Browse files Browse the repository at this point in the history
- result array returns the same information for forward and reverse geocoding (closes #35)
- result array return points of interest string array for iOS and Android (closes #38)
    - results[0].areasOfInterest = ["Brandenburger Tor"]
- update dependency 'cordova-plugin-add-swift-support' to 1.7.2
  • Loading branch information
sebastianbaar committed Mar 10, 2019
1 parent 72f0f60 commit 56b4554
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 34 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# 3.2.0 (2019-03-10)

- result array returns the same information for forward and reverse geocoding (closes #35)
- result array return points of interest string array for iOS and Android (closes #38)
- results[0].areasOfInterest = ["Brandenburger Tor"]
- update dependency 'cordova-plugin-add-swift-support' to 1.7.2

## ** BREAKING CHANGES **
- replace __NativeGeocoderForwardResult__ with __NativeGeocoderResult__
- replace __NativeGeocoderReverseResult__ with __NativeGeocoderResult__

# 3.1.3 (2018-11-12)

Android: return empty String if Address property is null (closes #34)
Expand Down
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,21 @@ All available `options` attributes:
| `maxResults` | `Number` | 1 | Optional. Min value: 1, max value: 5. |

### Array<Result>
Conforms to [Apple's](https://developer.apple.com/documentation/corelocation/clplacemark) and [Android's](https://developer.android.com/reference/android/location/Address.html) reverse geocoder's result arrays.
Conforms to [Apple's](https://developer.apple.com/documentation/corelocation/clplacemark) and [Android's](https://developer.android.com/reference/android/location/Address.html) geocoder's result arrays.

| Value | Type |
|-------------|-----------
| `latitude` | `String` |
| `longitude` | `String` |
| `countryCode` | `String` |
| `postalCode` | `String` |
| `administrativeArea` | `String` |
| `subAdministrativeArea` | `String` |
| `locality` | `String` |
| `subLocality` | `String` |
| `thoroughfare` | `String` |
| `subThoroughfare` | `String` |
| `subThoroughfare` | `String` |
| `areasOfInterest` | `Array<String>` |

### Example
```js
Expand Down Expand Up @@ -98,11 +101,21 @@ All available `options` attributes:
| `maxResults` | `Number` | 1 | Optional. Min value: 1, max value: 5. |

### Array<Result>
Conforms to [Apple's](https://developer.apple.com/documentation/corelocation/clplacemark) and [Android's](https://developer.android.com/reference/android/location/Address.html) geocoder's result arrays.

| Value | Type |
|-------------|-----------
| `latitude` | `String` |
| `longitude` | `String` |

| `latitude` | `String` |
| `longitude` | `String` |
| `countryCode` | `String` |
| `postalCode` | `String` |
| `administrativeArea` | `String` |
| `subAdministrativeArea` | `String` |
| `locality` | `String` |
| `subLocality` | `String` |
| `thoroughfare` | `String` |
| `subThoroughfare` | `String` |
| `areasOfInterest` | `Array<String>` |

### Example
```js
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.1.3",
"version": "3.2.0",
"description": "Cordova plugin for native forward and reverse geocoding",
"cordova": {
"id": "cordova-plugin-nativegeocoder",
Expand Down
4 changes: 2 additions & 2 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.1.3" 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.2.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 @@ -43,7 +43,7 @@
<string>$LOCATION_WHEN_IN_USE_DESCRIPTION</string>
</config-file>

<dependency id="cordova-plugin-add-swift-support" version="1.7.1"/>
<dependency id="cordova-plugin-add-swift-support" version="1.7.2"/>
</platform>

</plugin>
24 changes: 19 additions & 5 deletions src/android/NativeGeocoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ private void reverseGeocode(double latitude, double longitude, JSONObject option

// https://developer.android.com/reference/android/location/Address.html
JSONObject placemark = new JSONObject();
placemark.put("latitude", !String.valueOf(address.getLatitude()).isEmpty() ? address.getLatitude() : "");
placemark.put("longitude", !String.valueOf(address.getLongitude()).isEmpty() ? address.getLongitude() : "");
placemark.put("countryCode", address.getCountryCode() != null ? address.getCountryCode() : "");
placemark.put("countryName", address.getCountryName() != null ? address.getCountryName() : "");
placemark.put("postalCode", address.getPostalCode() != null ? address.getPostalCode() : "");
Expand All @@ -108,6 +110,7 @@ private void reverseGeocode(double latitude, double longitude, JSONObject option
placemark.put("subLocality", address.getSubLocality() != null ? address.getSubLocality() : "");
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());

resultObj.put(placemark);
}
Expand Down Expand Up @@ -166,10 +169,22 @@ private void forwardGeocode(String addressString, JSONObject options, CallbackCo
String longitude = String.valueOf(address.getLongitude());

if (!latitude.isEmpty() && !longitude.isEmpty()) {
JSONObject coordinates = new JSONObject();
coordinates.put("latitude", latitude);
coordinates.put("longitude", longitude);
resultObj.put(coordinates);
// https://developer.android.com/reference/android/location/Address.html
JSONObject placemark = new JSONObject();
placemark.put("latitude", latitude);
placemark.put("longitude", longitude);
placemark.put("countryCode", address.getCountryCode() != null ? address.getCountryCode() : "");
placemark.put("countryName", address.getCountryName() != null ? address.getCountryName() : "");
placemark.put("postalCode", address.getPostalCode() != null ? address.getPostalCode() : "");
placemark.put("administrativeArea", address.getAdminArea() != null ? address.getAdminArea() : "");
placemark.put("subAdministrativeArea", address.getSubAdminArea() != null ? address.getSubAdminArea() : "");
placemark.put("locality", address.getLocality() != null ? address.getLocality() : "");
placemark.put("subLocality", address.getSubLocality() != null ? address.getSubLocality() : "");
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());

resultObj.put(placemark);
}
}
catch (RuntimeException e) {
Expand Down Expand Up @@ -261,7 +276,6 @@ private Geocoder createGeocoderWithOptions(NativeGeocoderOptions geocoderOptions
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
locale = Locale.forLanguageTag(geocoderOptions.defaultLocale);
} else {
locale = Locale.ENGLISH;
String parts[] = geocoderOptions.defaultLocale.split("[-_]", -1);
if (parts.length == 1)
locale = new Locale(parts[0]);
Expand Down
45 changes: 31 additions & 14 deletions src/ios/NativeGeocoder.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import CoreLocation

struct NativeGeocoderReverseResult: Encodable {
struct NativeGeocoderResult: Encodable {
var latitude: String?
var longitude: String?
var countryCode: String?
var countryName: String?
var postalCode: String?
Expand All @@ -10,11 +12,7 @@ struct NativeGeocoderReverseResult: Encodable {
var subLocality: String?
var thoroughfare: String?
var subThoroughfare: String?
}

struct NativeGeocoderForwardResult: Encodable {
var latitude: String?
var longitude: String?
var areasOfInterest: [String]?
}

struct NativeGeocoderError {
Expand All @@ -28,8 +26,8 @@ struct NativeGeocoderOptions: Decodable {
}

@objc(NativeGeocoder) class NativeGeocoder : CDVPlugin {
typealias ReverseGeocodeCompletionHandler = ([NativeGeocoderReverseResult]?, NativeGeocoderError?) -> Void
typealias ForwardGeocodeCompletionHandler = ([NativeGeocoderForwardResult]?, NativeGeocoderError?) -> Void
typealias ReverseGeocodeCompletionHandler = ([NativeGeocoderResult]?, NativeGeocoderError?) -> Void
typealias ForwardGeocodeCompletionHandler = ([NativeGeocoderResult]?, NativeGeocoderError?) -> Void
private static let MAX_RESULTS_COUNT = 5

// MARK: - REVERSE GEOCODE
Expand Down Expand Up @@ -111,11 +109,15 @@ struct NativeGeocoderOptions: Decodable {

if let placemarks = placemarks {
let maxResultObjects = placemarks.count >= maxResults ? maxResults : placemarks.count
var resultObj = [NativeGeocoderReverseResult]()
var resultObj = [NativeGeocoderResult]()

for i in 0..<maxResultObjects {
// https://developer.apple.com/documentation/corelocation/clplacemark
let placemark = NativeGeocoderReverseResult(
let latitude = placemarks[i].location?.coordinate.latitude
let longitude = placemarks[i].location?.coordinate.longitude
let placemark = NativeGeocoderResult(
latitude: (latitude != nil) ? "\(String(describing: latitude))" : "",
longitude: (longitude != nil) ? "\(String(describing: longitude))" : "",
countryCode: placemarks[i].isoCountryCode ?? "",
countryName: placemarks[i].country ?? "",
postalCode: placemarks[i].postalCode ?? "",
Expand All @@ -124,7 +126,8 @@ struct NativeGeocoderOptions: Decodable {
locality: placemarks[i].locality ?? "",
subLocality: placemarks[i].subLocality ?? "",
thoroughfare: placemarks[i].thoroughfare ?? "",
subThoroughfare: placemarks[i].subThoroughfare ?? ""
subThoroughfare: placemarks[i].subThoroughfare ?? "",
areasOfInterest: placemarks[i].areasOfInterest ?? []
)
resultObj.append(placemark)
}
Expand Down Expand Up @@ -214,14 +217,28 @@ struct NativeGeocoderOptions: Decodable {

if let placemarks = placemarks {
let maxResultObjects = placemarks.count >= maxResults ? maxResults : placemarks.count
var resultObj = [NativeGeocoderForwardResult]()
var resultObj = [NativeGeocoderResult]()

for i in 0..<maxResultObjects {
if let latitude = placemarks[i].location?.coordinate.latitude,
let longitude = placemarks[i].location?.coordinate.longitude {

let coordinates = NativeGeocoderForwardResult(latitude: "\(latitude)", longitude: "\(longitude)")
resultObj.append(coordinates)
// https://developer.apple.com/documentation/corelocation/clplacemark
let placemark = NativeGeocoderResult(
latitude: "\(latitude)",
longitude: "\(longitude)",
countryCode: placemarks[i].isoCountryCode ?? "",
countryName: placemarks[i].country ?? "",
postalCode: placemarks[i].postalCode ?? "",
administrativeArea: placemarks[i].administrativeArea ?? "",
subAdministrativeArea: placemarks[i].subAdministrativeArea ?? "",
locality: placemarks[i].locality ?? "",
subLocality: placemarks[i].subLocality ?? "",
thoroughfare: placemarks[i].thoroughfare ?? "",
subThoroughfare: placemarks[i].subThoroughfare ?? "",
areasOfInterest: placemarks[i].areasOfInterest ?? []
)
resultObj.append(placemark)
}
}

Expand Down
13 changes: 6 additions & 7 deletions www/NativeGeocoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var exec = require('cordova/exec');

/**
* Reverse geocode a given latitude and longitude to find location address.
* @param {*} success {NativeGeocoderReverseResult[]} Success callback containing array of result objects
* @param {*} success {NativeGeocoderResult[]} Success callback containing array of result objects
* @param {*} error Error callback
* @param {*} latitude {number} The latitude
* @param {*} longitude {number} The longitude
Expand All @@ -14,7 +14,7 @@ exports.reverseGeocode = function(success, error, latitude, longitude, options)

/**
* Forward geocode a given address to find coordinates.
* @param {*} success {NativeGeocoderForwardResult[]} Success callback containing array of result objects
* @param {*} success {NativeGeocoderResult[]} Success callback containing array of result objects
* @param {*} error Error callback
* @param {*} addressString {string} The address to be geocoded
* @param {*} options {NativeGeocoderOptions} The options
Expand All @@ -24,7 +24,9 @@ exports.forwardGeocode = function(success, error, addressString, options) {
};

/*
NativeGeocoderReverseResult:
NativeGeocoderResult:
- latitude
- longitude
- countryCode
- postalCode
- administrativeArea
Expand All @@ -33,10 +35,7 @@ NativeGeocoderReverseResult:
- subLocality
- thoroughfare
- subThoroughfare
NativeGeocoderForwardResult:
- latitude
- longitude
- areasOfInterest
NativeGeocoderOptions:
- useLocale = true
Expand Down

0 comments on commit 56b4554

Please sign in to comment.