我正在使用angular2-google-mapsAngular2的最新版本.我试图将一些本地地图组件功能转换为自己文件中的服务maps.service.ts.例如:
map.component.ts
getGeoLocation(lat: number, lng: number) {
if (navigator.geolocation) {
let geocoder = new google.maps.Geocoder();
let latlng = new google.maps.LatLng(lat, lng);
let request = { latLng: latlng };
geocoder.geocode(request, (results, status) => {
if (status == google.maps.GeocoderStatus.OK) {
let result = results[0];
if (result != null) {
this.fillInputs(result.formatted_address);
} else {
alert("No address available!");
}
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
变成这样的东西: maps.service.ts
getGeoLocation(lat: number, lng: number): Observable<google.maps.GeocoderResult[]> {
let geocoder = new google.maps.Geocoder();
let latlng = new …Run Code Online (Sandbox Code Playgroud) google-maps typescript angular-cli angular2-google-maps angular
是否有任何标准格式可以将地址字符串提供给Google GeoCoding API,以便在地图上获得最准确的结果.
对于Eg.以下查询未给出正确的结果.
http://maps.googleapis.com/maps/api/geocode/xml?address=bloom,Bloomfield,CT,06002,USA&sensor=false
谢谢
Mandeep
我将如何从地址获取纬度和经度?
我目前的方法是将地址传递给 google maps api:
getLocationJson(term: string) {
var term = "Mechelsesteenweg+64";
return this._http.get('https://maps.googleapis.com/maps/api/geocode/json?address=Someroad+64&key=AIzkeystuffjXDm6eU5mPP9Nczg')
.subscribe((json: any) => {
var obj = JSON.parse(json);
var jsonParsed = obj["results"];
});
}
Run Code Online (Sandbox Code Playgroud)
但我觉得这不是正确的方法。我可以为此使用地理编码器吗?就像是:
getGeoLocation(address: string) {
let geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latlng = google.maps.location.LatLng();
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
Run Code Online (Sandbox Code Playgroud)
提前致谢。(基本上是这个问题,但反过来了)