2012-08-13 454 views
0

Possible Duplicate:
Undefined is not a function, Google Geolocation未定義在谷歌地圖API V3反向地理編碼

我試圖讓谷歌扭轉位置的地理編碼,它返回我不確定,但是,CONSOLE.LOG它顯示的地址

這是獲得功能值

function getLatLng(latlng, callback) { 
    var codedAddress; 

    geocoder.geocode({'latLng': new google.maps.LatLng(40.730885,-73.997383)}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      codedAddress = results[1].formatted_address; 
      console.log("codedAddress 1 = "+codedAddress); 
     } else { 
      alert("There was a problem with the map"); 
     } 
     console.log("codedAddress 2 = "+codedAddress); 
    }); 
    callback(codedAddress); 
} 

,這是我的初始化代碼

function initialize() { 
    var mapOptions = { 
     zoom: 11, 
     center: new google.maps.LatLng(5.386, 100.245), 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 

    map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions); 

    var location_coords = [ 
     <?php echo $output; ?> 
    ]; 

    var location_status = [ 
     <?php echo $status; ?> 
    ]; 

    var location_users = [ 
     <?php echo $users; ?> 
    ]; 

    var location_date = [ 
     <?php echo $date; ?> 
    ]; 

    for(i=0;i<location_coords.length;i++) { 
     var traffic_options = { 
     strokeColor: '#A52A2A', 
     strokeOpacity: 0.8, 
     strokeWeight: 1, 
     fillColor: getColor(location_status[i]), 
     fillOpacity: 0.5, 
     map: map, 
     center: location_coords[i], 
     radius: 300 
     }; 
     cityCircle = new google.maps.Circle(traffic_options); 

    barr[i] = new Object; 

    var marker = new google.maps.Marker({ 
     position: location_coords[i], 
     map: map, 
    }); 

    barr[i].marker = marker; 
    barr[i].html = "<div><span style='font-size:9px'>Reported by " + location_users[i] + " on " + location_date[i] + "</span>" + 
     "Traffic Location: " + getLatLng(location_coords[i], function(codedAddress) { return codedAddress; }) + 
     "<p>Traffic Condition: " + getCondition(location_status[i]) + "</p></div>"; 

    barr[i].infoWindow = new google.maps.InfoWindow({ 
     content: barr[i].html 
    }); 

    barr[i].listener = makeClosure(i, barr[i].marker); 
    } 
} 

問題是這種說法

"Traffic Location: " + getLatLng(location_coords[i], function(codedAddress) { return codedAddress; }) 

我如何獲得的價值在這裏,而不是「未定義」內

謝謝

+0

getLatLng()定義在哪裏? – 2012-08-13 04:06:49

+0

低於初始化函數 – user1594367 2012-08-13 04:12:02

回答

0

您需要將回調成爲谷歌地圖內回調功能:

function getLatLng(latlng, callback) { 
    var codedAddress; 

    geocoder.geocode({'latLng': new google.maps.LatLng(40.730885,-73.997383)}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     codedAddress = results[1].formatted_address; 
     console.log("codedAddress 1 = "+codedAddress); 
     } else { 
     alert("There was a problem with the map"); 
     } 
     console.log("codedAddress 2 = "+codedAddress); 
     callback(codedAddress); //moved here 
    }); 
    //it was here 
} 

因爲您在Google API填充變量之前調用了回調函數。

編輯: 您還需要修改寫出位置的代碼,因爲假定函數調用是同步的。

例如是這樣的:

getLatLng(location_coords[i], function(codedAddress) { 
    barr[i].html = "<div><span style='font-size:9px'>Reported by " + location_users[i] + " on " + location_date[i] + "</span>" + 
     "Traffic Location: " + codedAddress; + 
     "<p>Traffic Condition: " + getCondition(location_status[i]) + "</p></div>"; 
}); 
+0

編輯爲包含函數的使用 – 2012-08-13 04:26:49

+0

未捕獲的類型錯誤:無法在getLatLng塊內設置未定義的屬性'html'。你知道爲什麼嗎? – user1594367 2012-08-13 04:50:10

+0

您需要確保'bar [i]'變量在範圍內(同時通過使用單獨的函數或閉包來確保'i'是當前值)。您可能想要了解異步函數和同步函數之間的區別(您期望後者能從Google API中獲得前者) – 2012-08-13 08:16:56

0

在getLatLng()(DOH,它就在那裏......),你需要移動到callback(codedAddress);地理編碼調用中(和使用經緯度參數):

function getLatLng(latlng, callback) { 
    var codedAddress; 

    geocoder.geocode({'latLng': latlng}, 
    function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     codedAddress = results[1].formatted_address; 
     console.log("codedAddress 1 = "+codedAddress); 
     } else { 
     alert("There was a problem with the map"); 
     } 
     console.log("codedAddress 2 = "+codedAddress); 
    } 

    callback(codedAddress); 
);  
} 

這函數將返回void,所以你的回調應該初始化barr對象。