문자열 제공에서 좌표를 얻기 위해 이온 네이티브 지오 코더를 사용하고 있습니다. 국가는 국가에 제공된 문자열입니다. 좌표는 아래 코드에서 필요한 위도와 경도를 제공합니다. variable coordinatesData는 함수 내에서 위도와 경도를 포함하는 실제 결과를 제공합니다. 함수 외부에서 coordinatesData 를 인쇄하려고 하면 undefined가 제공됩니다.
this.nativeGeocoder.forwardGeocode(Country).then((coordinates: NativeGeocoderForwardResult[])=> {
let coordinatesData=JSON.stringify(coordinates, null, 2)
this.map.animateCamera( {
target: {
lat: coordinates[0].latitude,
lng: coordinates[0].longitude
}
, zoom: 4, duration: 500
}
);
let marker: Marker=this.map.addMarkerSync( {
position: {
lat: parseFloat(coordinates[0].latitude),
lng: parseFloat(coordinates[0].longitude)
}
, animation: GoogleMapsAnimation.DROP
}
);
}
) .catch((error: any)=> (error));
console.log(coordinatesData) // undefined
함수 내부에서 얻을 때 함수 외부의 변수 좌표 데이터에서 동일한 결과를 원합니다.
이렇게 해보세요. let
값은 함수 외부에서 사용할 수 없으므로var
var coordinatesData= ""; //define the variable outside your call.
this.nativeGeocoder.forwardGeocode(Country).then((coordinates: NativeGeocoderForwardResult[])=> {
coordinatesData=JSON.stringify(coordinates, null, 2);
this.map.animateCamera( {
target: {
lat: coordinates[0].latitude,
lng: coordinates[0].longitude
}
, zoom: 4, duration: 500
}
);
let marker: Marker=this.map.addMarkerSync( {
position: {
lat: parseFloat(coordinates[0].latitude),
lng: parseFloat(coordinates[0].longitude)
}
, animation: GoogleMapsAnimation.DROP
}
);
}
) .catch((error: any)=> (error));
console.log(coordinatesData) // undefined
업데이트 된 답변 :
var coordinatesData = this.nativeGeocoder.forwardGeocode(Country).then((coordinates: NativeGeocoderForwardResult[])=> {
this.map.animateCamera( {
target: {
lat: coordinates[0].latitude,
lng: coordinates[0].longitude
}
, zoom: 4, duration: 500
}
);
let marker: Marker=this.map.addMarkerSync( {
position: {
lat: parseFloat(coordinates[0].latitude),
lng: parseFloat(coordinates[0].longitude)
}
, animation: GoogleMapsAnimation.DROP
}
);
logData(JSON.stringify(coordinates, null, 2));
}
) .catch((error: any)=> (error));
function logData(coordinatesData){
console.log(coordinatesData) // undefined
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다