从谷歌地图 API 中的地址获取 latlng



如何从地址获取纬度和经度?

我目前的方法是将地址传递给谷歌地图 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"];
    });
}

但我觉得这不是正确的方法。我可以为此使用地理编码器吗?像这样:

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);
        }
    });
}

提前谢谢。(基本上是这个问题,但反过来了(

我回答了您链接的问题,这是对我有用的方法:

getGeoLocation(address: string): Observable<any> {
    console.log('Getting address: ', address);
    let geocoder = new google.maps.Geocode();
    return Observable.create(observer => {
        geocoder.geocode({
            'address': address
        }, (results, status) => {
            if (status == google.maps.GeocoderStatus.OK) {
                observer.next(results[0].geometry.location);
                observer.complete();
            } else {
                console.log('Error: ', results, ' & Status: ', status);
                observer.error();
            }
        });
    });
}

最新更新