【发布时间】:2023-12-13 14:19:01
【问题描述】:
首先感谢您抽出时间帮助我,感谢您的努力。
我在使用 google maps api,JavaScript 版本 3 时遇到问题。
我写了以下代码
$('.adr').ready(function(){
initialize();
})
function initialize() {
var myLatlng = codeAddress();
var myOptions = {
zoom: 14,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.SATELLITE
};
var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
}
function codeAddress()
{
var geocoder = new google.maps.Geocoder();
var address;
var street = cropAdr($(".street-address").text());
var city = cropAdr($(".locality").text());
var state = cropAdr($(".region").text());
var zip = cropAdr($(".zip").text());
address = street + ", " + city + ", " + state + ", " + zip;
geocoder.geocode( {'address': address}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
var latlng = results[0].geometry.location;
return latlng;
}
else
{
alert("Geocode was not successful for the following reason: " + status);
return null;
}
});
}
function cropAdr(args)
{
var index = args.indexOf(":");
var value = args.substr(index+1);
return value;
}
但它不起作用。
我已经检查了“results[0].geometry.location”返回的值及其完美,所以地址操作有效。 “results[0].geometry.location”是一个 google.maps.Latlng 对象,但我试图将坐标剥离为字符串,然后创建一个新的 google.maps.Latlng 对象但没有骰子。
但是,如果我手动复制该字符串并将值粘贴到“var myLatlng = new google.maps.Latlng(Paste Copied String here!)”中,整个事情就可以了!!
我看不出这个脚本还有什么问题(对于 Jquery 和 Javascritpt 的混乱表示歉意)。
【问题讨论】:
标签: javascript google-maps-api-3