gpt4 book ai didi

google-maps - 如何在Google Maps API中使用邮政编码代替Lat和Lng

转载 作者:行者123 更新时间:2023-12-03 12:41:36 28 4
gpt4 key购买 nike

我想知道是否有一种方法,可以使用此代码段或地理编码请求来使用邮政编码代替纬度和经度。

    <script type="text/javascript">
function initialize() {
var latlng = new google.maps.LatLng(22.1482635,-100.9100755);
// var latlang = new google.maps.zipcode(7845); <- Is there a way to do that?
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title:"zipcode"
});

}
</script>

最佳答案

您可以使用Google GeoCode API来请求邮政编码的纬度和经度,然后从那里去。

如果您想直接执行此操作或通过其他某种方式来执行此操作,API请求网址将如下所示

http://maps.googleapis.com/maps/api/geocode/json?address=50323&sensor=false

其中50323是您的邮政编码。

或者,您可以使用Google Javascript API通过jQuery完成所有操作。

var geocoder; //To use later
var map; //Your map
function initialize() {
geocoder = new google.maps.Geocoder();
//Default setup
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}

//Call this wherever needed to actually handle the display
function codeAddress(zipCode) {
geocoder.geocode( { 'address': zipCode}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
//Got result, center the map and put it out there
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}

关于google-maps - 如何在Google Maps API中使用邮政编码代替Lat和Lng,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11147803/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com