从函数返回GMap位置对象
returning GMap location object from function
我正在尝试为地图工具做一个简单的地理编码函数。地理编码很好,但我希望将location对象作为地理编码函数的返回值传递回来。
类似这样的东西:
function codeAddress(address) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var location = results[0].geometry.location;
console.dir(location);
return location;
} else {
return false;
}
});
}
location项的console.dir显示了预期的location对象,因此该函数被调用并成功返回数据。
该函数由另一个进程调用,然后该进程将构建标记。
if (coordinates = codeAddress(myaddress){
// do stuff
}
然而,coordinates
变量的计算结果始终为未定义,因此永远不会满足"做事"的条件。
我知道我可能遗漏了一些关于坐标var定义的非常明显的东西,但我不确定它是什么
谢谢你的帮助。
基本代码位于:http://jsfiddle.net/2TXZ4/3/尽管不管出于什么原因地图都没画出来。
geocode
方法是异步的,因此您需要在向该方法提供的回调中进行任何处理,或者您可以向codeAddress
方法提供回调,就像下面描述的类似问题/答案一样(如何从Google Maps JavaScript地理编码回调返回变量?)。在下面的代码中,我将您在doStuff
函数中所做的操作移到了geocode
函数的回调中——它应该可以工作,但我无法使用您提供的jfiddle链接来测试它(可能是由于Google Maps API键)。我希望这能有所帮助!
function codeAddress(address) {
geocoder.geocode({
'address': address
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var location = results[0].geometry.location;
console.log(location);
map.setCenter(location);
var marker = new google.maps.Marker({
map: map,
position: location
});
} else {
alert("Geocode was not successful for " + address);
}
});
}
window.doStuff = function() {
var address = document.getElementById("address").value;
codeAddress(address);
}
if (coordinates = codeAddress(myaddress)
-这是一个赋值,而不是比较。
好的,这里有一个疯狂的破解(确实有效):
<script type="text/javascript"> var geocoder; var loc; var l = false;
function initialize() {
geocoder = new google.maps.Geocoder();
var mapDiv = document.getElementById('map_canvas');
map = new google.maps.Map(mapDiv, {
center: new google.maps.LatLng(-34.397, 150.644),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
}); }
function codeAddress(address) {
geocoder.geocode({
'address': address
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
loc = results[0].geometry.location; l = true;
console.log(loc); //alert (loc);
//return loc; doStuff(loc)
} else {
return false;
}
}); }
function doStuff(geo_marker){ if(l==false){
var thisaddress = document.getElementById("address").value;
var myResult = codeAddress(thisaddress); }else{
if (loc) {
map.setCenter(loc);
var marker = new google.maps.Marker({
map: map,
position: loc
});
l = false; //important to use function more than once :))
} else {
alert("Geocode was not successful");
} }
};
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<body>
<div>
<input id="address" type="textbox" value="Sydney, NSW">
<input type="button" value="Geocode" onclick="window.doStuff()">
</div>
<div id="map_canvas" style="height:600px; width: 500px; position: absolute;
top: 80px"> </div>
</body>
不过,我确实认为在一个函数中进行地理编码并向映射添加标记更有意义,以避免在函数之间来回调用。
相关文章:
- 通过拖动可以更改三维模型对象在Three.js场景中的位置
- 如何在鼠标悬停时将对象从起始位置移动到结束位置,然后在鼠标悬停后再次移动
- 使用地理坐标,对更靠近用户的对象进行排序'的位置
- 块相对于父对象的位置
- 使用AngularJS修改对象位置只需一次
- jQuery拖动到相对位置的可排序列表中时出现可拖动辅助对象位置问题
- 为什么 javascript 返回对象位置 true 或 false
- 在MongoDB中排序后获取对象的位置
- 是否可以模拟 qUnit 测试的窗口位置对象
- 查找javascript对象中元素的位置
- 位置对象Javascript
- javascript/谷歌地图:将位置对象转换为字符串
- FF 13,IE 9:JSON 字符串化/地理位置对象
- 如何使用位置对象来解析 URL,而无需在 javascript 中重定向页面
- 定义位置对象和URL参数的正确URL语法是什么
- 防止固定位置对象在特定位置随页面滚动
- 相位定时器不改变位置对象,只改变位置值
- HTML5创建地理位置对象
- 从函数返回GMap位置对象
- Java等价于Javascript位置对象