0

私の問題はこれです。私はjavascriptが初めてです。Google マップ API (latlng に基づいて位置を返す) への非同期呼び出しを行う関数があります。コード内のこの関数はMarkerCreaterPlusLocationです。この呼び出しは、私のコードでMarkerCreaterという名前の別の関数で必要な値を返します。しかし問題は、MarkerCreaterPlusLocationが値を返すまでMarkerCreaterが停止しないことです。

この問題を克服するために、非同期関数が値を返すときにMarkerCreaterのコールバックを使用して実行しようとしました

構造は次のとおりです。

google.maps.event.addListener(map, 'click',addLatLng); //This code attaches the function to Listener



function addLatLng(event) {
        path = poly.getPath();
        path.push(event.latLng);
        MarkerCreaterPlusLocation(event.latLng,MarkerCreater);//MarkerCreater is the callback function
}




function MarkerCreaterPlusLocation(input,callback){
    location="l";
    geocoder.geocode({'latLng': input}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      if (results[1]) {
        location=results[1].formatted_address;
        callback(location,input);//I call the callback function upon the success of the result or otherwise
      } else {
        location="l"; 
        callback(location,input);
      }
    } else {
      location="l";
      callback(location,input);
    }
    });
}




function MarkerCreater(l,x){

    var marker = new google.maps.Marker({
        position: x,
        title: '#' + path.getLength()+l,
        icon: 'images/beachflag.png',
        map: map
});
    ///Some more javascript code 
}

これは機能していないように見えるので、ここで間違いを犯していると思います。むしろ、404 エラーが発生するため、理解するのがさらに難しくなります。助けてください

4

1 に答える 1