cuốn sách gpt4 ai đã làm

javascript - 如何返回 google map 标记的城市、街道和邮政编码?

In lại Tác giả: Walker 123 更新时间:2023-11-28 08:28:29 26 4
mua khóa gpt4 Nike

我有这段代码,用于填充表单上的纬度和经度的两个文本输入。我想知道是否有办法检索城市、街道和邮政编码,以便我也可以使用它们填充到表单中?

google.maps.event.addListener(marker, 'dragend', function(marker){
var latLng = marker.latLng;
$latitude.value = latLng.lat();
$longitude.value = latLng.lng();
});

Ví dụ:

$zipcode.value = latLng.postal_code();
$street.value = latLng.street_address();
$city.value = latLng.city();

这是我的完整代码:

function selectState(state_id){
if(state_id!="-1"){
loadData('city',state_id);
var e = document.getElementById("state");
var stateloc = e.options[e.selectedIndex].value;

var address = state_id + stateloc;

var $latitude = document.getElementById('latitude');
var $longitude = document.getElementById('longitude');
var latitude = 73.00636021320537;
var longitude = -23.46687316894531;

var zoom = 10;
geocoder = new google.maps.Geocoder();
var LatLng = new google.maps.LatLng(latitude, longitude);

var mapOptions = {
zoom: zoom,
center: LatLng,
panControl: false,
zoomControl: true,
scaleControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
}

var map = new google.maps.Map(document.getElementById('map'),mapOptions);



if (geocoder) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
map.setCenter(results[0].geometry.location);




var marker = new google.maps.Marker({
position: results[0].geometry.location,
map: map,
title: 'Drag Me!',
draggable: true
});

google.maps.event.addListener(marker, 'dragend', function(marker){
var latLng = marker.latLng;
$latitude.value = latLng.lat();
$longitude.value = latLng.lng();
});



} khác {
alert("No results found");
}
} khác {
alert("Geocode was not successful for the following reason: " + status);
}

});
}


}else{$("#city_dropdown").html("");
}
}

câu trả lời hay nhất

您面临的问题最简单的解决方法是调用反向地理编码器,该编码器位于

https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false

一个非常简单的 php 脚本可能如下所示:

  $value=json_decode(file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false"));
echo $value->results[0]->formatted_address;
?>

这会产生输出

277 Bedford Avenue, Brooklyn, NY 11211, USA

我建议(根据您的用户名)您从“保持简单”开始 - 从页面中删除所有代码,除了需要经度/纬度输入并调用上述代码的代码之外。向自己证明您可以重新创建此输出(除了格式化地址之外,还有用于数字、街道等的单独字段 - 请参阅返回的完整结构)。然后添加进一步的复杂性( map 、图钉、下拉菜单……)。

使用上面的示例,

var latitude = 73.00636021320537;
var longitude = -23.46687316894531;

返回的地址是

Greenland

我想这就是当你将一根大头针插在荒野中央时会发生的情况:

nhập mô tả hình ảnh ở đây

UPDATE 如果您只想使用 Javascript 来执行此操作,我们强烈建议您使用 jQuery 来获取 JSON 解析功能。示例脚本(加载页面后加载与原始示例相同的地址 - 关键是可以用 AJAX 调用替换的 httpGet 函数,以及 jQuery.parseJSON() 调用有助于将响应转化为可以从中提取有用信息的内容:




function ready()
{
var r = httpGet("https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false");
document.write(getAddress(r));
}

function httpGet(theUrl)
{
var xmlHttp = null;

xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false );
xmlHttp.send( null );
return xmlHttp.responseText;
}

function getAddress(response)
{
var obj = jQuery.parseJSON(response);
return obj.results[0].formatted_address;
}



Xem sitemap của VNExpress

在工作中查看此内容 http://www.floris.us/SO/geocode.html

关于javascript - 如何返回 google map 标记的城市、街道和邮政编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22138790/

26 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress