2012-05-21 222 views
0

什麼,我試圖做的是添加標記時,地址解析器完成解析到經緯度...這是我的代碼:谷歌地理編碼器+谷歌地圖添加標記

function parseLocation() { 
    var user1Location = "Mechelen, Belgium"; 
    var geocoder = new google.maps.Geocoder(); 
    //convert location into longitude and latitude 
    geocoder.geocode({ 
     address: user1Location 
    }, function (locResult) { 
     lat1 = locResult[0].geometry.location.lat(); 
     lng1 = locResult[0].geometry.location.lng(); 
     if (status == google.maps.GeocoderStatus.OK) { 
      $('#map_canvas').bind('init', function() { 
       $('#map_canvas').gmap('addMarker', { 
        'position': lat1 + ',' + lng1 
       }).click(function() { 
        $('#map_canvas').gmap('openInfoWindow', { 
         'content': lat1 + ',' + lng1 
        }, this); 
       }); 
      }); 
     } 
    }); 
} 

當我提醒中的變量初始化函數他們是未定義的,我知道這個geocoder是異步的,但我在等待狀態是好的...任何想法什麼是做錯了?

回答

2

試試吧。 Geocoder是異步的,但多次調用回調函數。

geocoder.geocode({"address":user1Location}, function(locResult, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     lat1 = locResult[0].geometry.location.lat(); 
     lng1 = locResult[0].geometry.location.lng(); 
    } 
}); 

這就是在相同的代碼鏈接工作。

http://jsfiddle.net/2jbTX/2/

+0

不工作o.O –

+0

那鏈路工作在相同的代碼。 http://jsfiddle.net/2jbTX/2/ – turankonan