我想在使用Nativescript角度获取当前位置坐标后获取位置详细信息,例如城市名称,街道,国家等。在Google反向地理编码中存在限制,因此我找到了一种解决方案,可以使用github的本机功能来实现,但存在指令问题。我正在使用nativescript-geolocation:^ 5.1.0并粘贴代码,如果有人可以建议如何实现的话。
file.ts
trackCurrentLocation(){
let parentScope = this;
geolocation.getCurrentLocation({
desiredAccuracy: Accuracy.high,
maximumAge: 5000,
timeout: 10000
}).then(function (loc) {
if (loc) {
parentScope.locationService.geocode(loc.latitude,loc.longitude)
.subscribe((result) => {
console.log(result);
}, (error) => {
console.log(error);
});
}
}, function (e) {
console.log("Error: " + (e.message || e));
});
}
location.service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from "@angular/common/http";
var application = require("tns-core-modules/application");
import { isAndroid, isIOS } from "tns-core-modules/platform";
import { android } from 'tns-core-modules/application/application';
@Injectable({
providedIn: 'root'
})
export class LocationService {
constructor(private http: HttpClient) { }
private createRequestHeader() {
// set headers here e.g.
let headers = new HttpHeaders({
"Content-Type": "application/json",
});
return headers;
}
geocode(lat,lon){
let position: any;
if(!args.location)
return new Promise(function (reject) {return reject("error")});
if (isAndroid) {
return new Promise(function(resolve, reject){
var locale = java.util.Locale.getDefault();
var geocoder = new android.location.Geocoder(application.android.currentContext, 'en_US');
var addresses = geocoder.getFromLocation(lat, lon, 1);
if (addresses != null && addresses.size() > 0) {
var address = addresses.get(0);
position = <any>{
latitude: address.getLatitude(),
longitude: address.getLongitude(),
subThoroughfare: address.getThoroughfare(),
thoroughfare: address.getSubThoroughfare(),
locality: address.getLocality(),
subLocality: address.getSubLocality(),
adminArea: address.getAdminArea(),
subAdminArea: address.getSubAdminArea(),
postalCode: address.getPostalCode(),
country: address.getCountryName(),
countryCode: address.getCountryCode(),
addressLines: []
}
for (var i = 0; i <= address.getMaxAddressLineIndex(); i++) {
position.addressLines.push(address.getAddressLine(i));
}
return position;
}
});
}
if (isIOS) {
return new Promise(function(resolve, reject){
let geocoder = new CLGeocoder();
geocoder.reverseGeocodeLocationCompletionHandler(
args.location.ios,
(placemarks, error) => {
if (error) {
console.log(error);
var newerror = new BaseError("error", "error");
return reject(newerror);
} else if (placemarks && placemarks.count > 0) {
let pm = placemarks[0];
let addressDictionary = pm.addressDictionary;
let address = ABCreateStringWithAddressDictionary(addressDictionary, false);
position = <any>{
latitude: args.location.latitude,
longitude: args.location.longitude,
subThoroughfare: addressDictionary.objectForKey('SubThoroughfare'),
thoroughfare: addressDictionary.objectForKey('Thoroughfare'),
locality: addressDictionary.objectForKey('City'),
subLocality: addressDictionary.objectForKey('SubLocality'),
adminArea: addressDictionary.objectForKey('State'),
subAdminArea: addressDictionary.objectForKey('SubAdministrativeArea'),
postalCode: addressDictionary.objectForKey('ZIP'),
country: addressDictionary.objectForKey('Country'),
countryCode: addressDictionary.objectForKey('CountryCode'),
addressLines: []
};
let lines = addressDictionary.objectForKey('FormattedAddressLines');
for (var i = 0; i < lines.count; i++) {
position.addressLines.push(lines[i]);
}
return resolve(position);
}
});
});
}
}
}