角度アプリで逆ジオコーディングを使用しています。角度を使用した逆ジオコーディング
必要なスクリプトはindex.htmlを
<script async defer src="https://maps.googleapis.com/maps/api/js">
</scrip>
およびコンポーネントファイルに追加されますが、この
import { Component } from '@angular/core';
declare const google: any;
export class MapComponent {
lat;
lng;
address;
constructor() {
this.locate();
}
public locate() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
position => {
this.lat = position.coords.latitude; // Works fine
this.lng = position.coords.longitude; // Works fine
let geocoder = new google.maps.Geocoder();
let latlng = new google.maps.LatLng(this.lat, this.lng);
let request = {
latLng: latlng
};
geocoder.geocode(request, (results, status) => {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0] != null) {
this.address = results[0].formatted_address; //<<<=== DOES NOT WORK, when I output this {{ address }} in the html, it's empty
console.log(this.address); //<<<=== BUT here it Prints the correct value to the console !!!
} else {
alert("No address available");
}
}
});
},
error => {
console.log("Error code: " + error.code + "<br /> Error message: " + error.message);
}
);
}
}
}
とコンポーネントのhtmlファイルのように見えますが、これはアドレス
出力になっています<div>{{ lat }}</div> // Works fine
<div>{{ lng }}</div> // Works fine
<div>{{ address }}</div> // Deosn't Work, EMPTY
しかし、それは常に空です。 ただし、この行
console.log(this.address);
正しい値が印刷されます。
あなたは私の命を救った:) –