UNPKG

@nativescript-community/geocoding

Version:

Provides access to native geocoding APIs for NativeScript apps (android.location.Geocoder for Android, CLGeocoder for iOS).

103 lines 4.54 kB
import { Device } from '@nativescript/core'; import { LOC_SEARCH_MAX_RESULTS, LocationBase } from './geocoding-common'; export function getLocationFromName(searchString) { return new Promise(function (resolve, reject) { getIosGeocoder().geocodeAddressStringCompletionHandler(searchString, (placemarks, error) => { if (error) { const clError = new Error('iOS CLGeocoder error : ' + error.localizedDescription); return reject(clError); } else if (placemarks && placemarks.count > 0) { resolve(locationFromCLPlacemark(placemarks.objectAtIndex(0))); } }); }); } export function getLocationListFromName(searchString, maxResCount) { return new Promise(function (resolve, reject) { if (!maxResCount || maxResCount < 0 || maxResCount > LOC_SEARCH_MAX_RESULTS) { maxResCount = LOC_SEARCH_MAX_RESULTS; } getIosGeocoder().geocodeAddressStringCompletionHandler(searchString, (placemarks, error) => { if (error) { const clError = new Error('iOS CLGeocoder error : ' + error.localizedDescription); return reject(clError); } else if (placemarks && placemarks.count > 0) { const maxRes = Math.min(placemarks.count, maxResCount); const res = new Array(); for (let i = 0; i < maxRes; i++) { res.push(locationFromCLPlacemark(placemarks.objectAtIndex(i))); } resolve(res); } }); }); } export function getFromLocation(latitude, longitude, maxResCount) { return new Promise(function (resolve, reject) { if (!maxResCount || maxResCount < 0 || maxResCount > LOC_SEARCH_MAX_RESULTS) { maxResCount = LOC_SEARCH_MAX_RESULTS; } const cllocation = CLLocation.alloc().initWithLatitudeLongitude(latitude, longitude); getIosGeocoder().reverseGeocodeLocationCompletionHandler(cllocation, (placemarks, error) => { if (error) { const clError = new Error('iOS CLGeocoder error : ' + error.localizedDescription); return reject(clError); } else if (placemarks && placemarks.count > 0) { const maxRes = Math.min(placemarks.count, maxResCount); const res = new Array(); for (let i = 0; i < maxRes; i++) { res.push(locationFromCLPlacemark(placemarks.objectAtIndex(i))); } resolve(res); } }); }); } function getVersionMaj() { return parseInt(Device.osVersion.split('.')[0], 10); } function locationFromCLPlacemark(pm) { const mVer = getVersionMaj(); const location = new Location(); location.latitude = pm.location?.coordinate?.latitude; location.longitude = pm.location?.coordinate?.longitude; if (mVer < 11) { const addressDictionary = pm.addressDictionary; location.subThoroughfare = addressDictionary.objectForKey('SubThoroughfare'); location.thoroughfare = addressDictionary.objectForKey('Thoroughfare'); location.locality = addressDictionary.objectForKey('City'); location.subLocality = addressDictionary.objectForKey('SubLocality'); location.administrativeArea = addressDictionary.objectForKey('State'); location.subAdministrativeArea = addressDictionary.objectForKey('SubAdministrativeArea'); location.postalCode = addressDictionary.objectForKey('ZIP'); location.country = addressDictionary.objectForKey('Country'); location.isoCountryCode = addressDictionary.objectForKey('CountryCode'); } else { location.name = pm.name; location.isoCountryCode = pm.ISOcountryCode; location.country = pm.country; location.postalCode = pm.postalCode; location.administrativeArea = pm.administrativeArea; location.subAdministrativeArea = pm.subAdministrativeArea; location.locality = pm.locality; location.subLocality = pm.subLocality; location.thoroughfare = pm.thoroughfare; location.subThoroughfare = pm.subThoroughfare; } location.ios = pm; return location; } let iosGeocoder; function getIosGeocoder() { if (!iosGeocoder) { iosGeocoder = new CLGeocoder(); } return iosGeocoder; } export class Location extends LocationBase { } //# sourceMappingURL=geocoding.ios.js.map