2012-08-13 172 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对象。