如何返回要在html页面上显示的城市名称?
我的函数
function getCity(latitude, longitude){
var geocoder;
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(latitude, longitude);
geocoder.geocode(
{'latLng': latlng},
function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
var add= results[0].formatted_address ;
var value=add.split(",");
count=value.length;
country=value[count-1];
state=value[count-2];
city=value[count-3];
alert("city name is: " + city);
return city;
}
}
}
);警报工作正常。
在HTML中,我这样调用这个函数:
<script> getCity("<%= map.latitude %>","<%= map.longitude %>")</script>提前Tks。
发布于 2016-08-01 09:42:05
将您的代码更改为:
<script> document.write(getCity("<%= map.latitude %>","<%= map.longitude %>"))</script>它会将Javascript函数返回的内容写入到文档中。
发布于 2016-08-01 09:46:26
在HTML中创建一个DIV,如下所示:
<div id="cityName"></div>在javascript中,将"alert“替换为以下代码:
document.getElementById("cityName").innerHTML(city);发布于 2016-08-01 09:57:09
如果警报工作正常,则意味着返回值为true。然后,您可以只调用返回文档体中任何位置的城市名称的函数。或者,由于函数只返回一个字符串,您只需将其赋给一个新变量,并向返回的函数城市字符串添加更多信息。
Like**函数city ()返回一个城市名值。
城市名=()+‘更多信息’;
**
https://stackoverflow.com/questions/38689928
复制相似问题