2016-11-25 122 views
0

我正在使用角度2谷歌地圖。
如何獲得地址像國家和pin從緯度,經度在angular2谷歌地圖與打字稿?如何從緯度,經度角2獲取完整地址

+0

這可能有助於http://stackoverflow.com/a/23344822/217408如果不是,請添加代碼,說明您嘗試了什麼,以及您失敗的位置(包含確切的錯誤消息) –

回答

1

使用sebm-google-map你可以這樣做:

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]; 
     let rsltAdrComponent = result.address_components; 
     let resultLength = rsltAdrComponent.length; 
     if (result != null) { 
      this.marker.buildingNum = rsltAdrComponent[resultLength-8].short_name; 
      this.marker.streetName = rsltAdrComponent[resultLength-7].short_name; 
     } else { 
      alert("No address available!"); 
     } 
     } 
    }); 
} 
} 

*請務必在全球公開google對象:declare var google: any;

然後:

mapClicked($event: MouseEvent) { 
    this.marker.lat = $event.coords.lat; 
    this.marker.lng = $event.coords.lng; 
    this.getGeoLocation(this.marker.lat, this.marker.lng);  
    console.log("Lat: " +this.marker.lat +" Long: " +this.marker.lng) 
} 

組件/ HTML:

<sebm-google-map [latitude]="marker.lat || centerLat" [longitude]="marker.lng || centerLng" [zoom]="8" (mapClick)="mapClicked($event)"> 
     <sebm-google-map-marker *ngIf="marker.lat" (markerClick)="clickedMarker(marker.label)" [latitude]="marker.lat" [longitude]="marker.lng" [markerDraggable]="marker.draggable" (dragEnd)="markerDragEnd(marker, $event)"> 
      <sebm-google-map-info-window [disableAutoPan]="true"> 
        {{marker.label}} 
      </sebm-google-map-info-window> 
     </sebm-google-map-marker> 
</sebm-google-map>