2

地名をジオコーディングしようとしていますが、関数を実行すると、

コンソールの「空の文字列」、

以下は私のコードです。なぜこれが起こるのでしょうか?

function getLatLong(address) 
{
    var geocoder = new google.maps.Geocoder();
    var result = "";
    geocoder.geocode( { 'address': address, 'region': 'uk' }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            result = results[0].geometry.location;
        } else {
            result = "Unable to find address: " + status;
        }
    });
    console.log(result);
}

アップデート

OK、コンソールに値を取得しました。要素の値を関数にプッシュしようとすると、未定義の戻り値が返されます。以下は完全なコードです。

function getLatLong(address) 
{
    var geocoder = new google.maps.Geocoder();
    var result = "";
    geocoder.geocode( { 'address': address, 'region': 'uk' }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            result = results[0].geometry.location;
            return result;
        } else {
            result = "Unable to find address: " + status;
            alert(result);
        }
    });
}

//getLatLong("YORK"); 

function loadScript(postcode){
    alert(postcode);
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize?"+postcode;
    document.body.appendChild(script);
   }

   /* load map on visitors location */
   function initialize(postcode){
     var myLatlng = new google.maps.LatLng(getLatLong(postcode));
     var myOptions = {
      center: myLatlng,
      zoom:13,
      disableDefaultUI: true,
      mapTypeId: google.maps.MapTypeId.ROADMAP
     }

     var map = new google.maps.Map(document.getElementById('map'), myOptions);
     google.maps.event.trigger(map, 'resize');
     map.setZoom(map.getZoom());
     var marker = new google.maps.Marker({
      position: myLatlng, 
      map: map,
      icon:'http://google-maps-icons.googlecode.com/files/home.png',
     });
    }

そして、それはこのように呼ばれます、

 $(function(){
    $("dd a, dt a").live("click", function(){
        var self = $(this);
        $("#overlay").fadeIn('slow');
        var targetProcent = 85;
        var targetWidth = $(window).width() * (targetProcent / 100);
        var targetHeight = $(window).height() * (targetProcent / 100);   
        var targetX = ($(window).width() - targetWidth) / 2;
        var targetY = ($(window).height() - targetHeight) / 2 + $(document).scrollTop();
        $('#lightbox').height(700);
        $('#lightbox').width(targetWidth);
        $('#lightbox').load(self.attr("href"));
        loadScript($("#postcode").val());
        //usePointFromPostcode(document.getElementById('postcode').value, placeMarkerAtPoint)
        $('#lightbox').css({
            "position": "absolute", 
            "top": targetY+"px", 
            "left": targetX+"px"
        }).fadeIn('slow');
        return false;
    });
    });

$("#postcode").val() は、load() の使用時に読み込まれる要素に関連しています

4

1 に答える 1

4

非同期呼び出しを行い、コンソールに出力します。コールはまだ返されていません

解決:

console.log をコールバックに移動します

編集: コールバックは、ジオコードに引数として渡す匿名関数です。その署名は、その関数の END に呼び出しをfunction(results, status)配置する必要があることです。console.log

あなたの編集のための編集:

スクリプトの URL が間違っているようです:

"http://maps.google.com/maps/api/js?sensor=false&callback=initialize?"+postcode

?postcode を付けても何も起こらないと確信しています。どちらかといえば、URLには1つだけを含める必要があります ? クエリ パラメータの開始前。郵便番号を関数に渡したいようですが、それinitializeを行う最も簡単な方法はグローバル変数を使用することです。

関数でグローバル変数を設定し、関数loadScriptでそれを読み取りますinitialize

于 2011-06-16T13:43:58.530 に答える