如何从Google Map API函数中获取javascript lat / lng变量 [英] How to get a javascript lat/lng variable out of a Google Map API function

查看:163
本文介绍了如何从Google Map API函数中获取javascript lat / lng变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我无法获取经纬度/经纬度变量在Geocode脚本中运行的函数之外。我使用的代码如下:

 < input id =addresstype =textboxvalue = > 
< script type =text / javascript>
var lat;
var lng;
函数addressurls(地址){
var address = document.getElementById(address)。value;
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address':address},函数postcodesearch(results,status)
{
if(status == google.maps.GeocoderStatus.OK)
{
var lat = results [0] .geometry.location.lat();
var lng = results [0] .geometry.location.lng();
}
else {
alert(Error);
}
return lat;
return lng;
});
var laturl =& lat =;
var lonurl =& lon =;
var postcode = document.getElementById(address)。value;
var addressurl =/ distributors-search?address =+ postcode + laturl + lat + lonurl + lng;
parent.location = addressurl;
}
< / script>
< input type =buttonvalue =Encodeonclick =addressurls('address'),parent.location = addressurl>

有谁知道如何解决它?

 < input id =addresstype =textboxvalue => 
< script type =text / javascript>
var lat;
var lng;
函数addressurls(地址){
var address = document.getElementById(address)。value;
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address':address},函数postcodesearch(results,status)
{
if(status == google.maps.GeocoderStatus.OK)
{
var lat = results [0] .geometry.location.lat();
var lng = results [0] .geometry.location.lng();
var laturl =& lat =;
var lonurl =& lon =;
var postcode = document.getElementById(address)。value;
var addressurl =/ distributors-search?address =+ postcode + laturl + lat + lonurl + lng;
parent.location = addressurl;
}
else {
alert(Error);
}
});

}
< / script>
< input type =buttonvalue =Encodeonclick =addressurls('address'),parent.location = addressurl>


I need to get the Lat/Lng from a Postcode, so I'm using Google Map/Geocode API.

I can't get the lat/lng variable out of the function running within the Geocode script. The code I'm using is below:

<input id="address" type="textbox" value="">
<script type="text/javascript">
var lat;
var lng;
function addressurls(address) {
var address = document.getElementById("address").value;
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address': address}, function postcodesearch(results, status) 
{   
  if (status == google.maps.GeocoderStatus.OK) 
  {
    var lat = results[0].geometry.location.lat();
    var lng = results[0].geometry.location.lng();
  }
  else {
    alert("Error");
  }
  return lat;
  return lng;
});
var laturl = "&lat=";
var lonurl = "&lon=";
var postcode = document.getElementById("address").value;
var addressurl = "/distributors-search?address=" + postcode + laturl + lat+ lonurl + lng;
parent.location=addressurl;
}
</script>
<input type="button" value="Encode" onclick="addressurls('address'), parent.location=addressurl">

Does anyone know how to fix it?

解决方案

Geocoding is asynchronous, you need to use the returned data in the callback function

<input id="address" type="textbox" value="">
<script type="text/javascript">
var lat;
var lng;
function addressurls(address) {
var address = document.getElementById("address").value;
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address': address}, function postcodesearch(results, status) 
{   
  if (status == google.maps.GeocoderStatus.OK) 
  {
    var lat = results[0].geometry.location.lat();
    var lng = results[0].geometry.location.lng();
    var laturl = "&lat=";
    var lonurl = "&lon=";
    var postcode = document.getElementById("address").value;
    var addressurl = "/distributors-search?address=" + postcode + laturl + lat+ lonurl + lng;
    parent.location=addressurl;
  }
  else {
    alert("Error");
  }
});

}
</script>
<input type="button" value="Encode" onclick="addressurls('address'), parent.location=addressurl">

这篇关于如何从Google Map API函数中获取javascript lat / lng变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆