gpt4 book ai didi

javascript - 对 Google map 上的地理位置进行地理编码会出现错误

转载 作者:行者123 更新时间:2023-12-02 20:01:18 26 4
gpt4 key购买 nike

当您点击“查找我”按钮时,它会找到您的位置,但问题出在地理编码部分。

它应该在“myaddress”div 中显示您所在位置的地址。但它给出了一个我不明白的错误。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
function initialize() {
var latlng = new google.maps.LatLng(0, 0);
var myOptions = {
zoom: 1,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("sbmr"),
myOptions);


}

/////////////////////////////////////////////////////////////////////////////

var initialLocation;
var siberia = new google.maps.LatLng(60, 105);
var browserSupportFlag = new Boolean();
var myposition;
var geocoder;

function geocode() {

var myOptions = {
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById("sbmr"), myOptions);

// Try W3C Geolocation (Preferred)
if(navigator.geolocation) {
browserSupportFlag = true;
navigator.geolocation.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);


/////////////////PROBLEM IS WITH THIS PART///////////////////
var latlng = new google.maps.LatLng(initialLocation);
geocoder = new google.maps.Geocoder();
geocoder.geocode(latlng, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1])
document.getElementById("myaddress").innerHTML = 'results[1].formatted_address'
}
else {
alert("Geocoder failed due to: " + status);
}
});
//////////////////////////////////////////////////
var marker = new google.maps.Marker({
position: initialLocation,
map: map,
animation: google.maps.Animation.BOUNCE,
title:"You're here."
});

map.setCenter(initialLocation);

map.setZoom(15);

//W3C olursa yapilanlar bitis
}, function() {
handleNoGeolocation(browserSupportFlag);
});
}
}//geocodefinish
</script>

<style type="text/css">
<!--
#sbmr {
position:absolute;
left:62px;
top:39px;
width:1098px;
height:649px;
z-index:1;
}
#myaddress {
position:absolute;
left:1185px;
top:219px;
width:286px;
height:318px;
z-index:2;
}
#apDiv1 {
position:absolute;
left:1189px;
top:70px;
width:264px;
height:117px;
z-index:3;
}
-->
</style>
</head>

<body onload="initialize()">



<div id="sbmr"></div>
<div id="myaddress">
<h2>My address:</h2>
<br />
</div>
<div id="apDiv1">
<label>
<input type="submit" name="button" id="button" value="Find Me" onclick="geocode()" />
</label>
</div>
</body>
</html>

最佳答案

Geocoder 将字符串地址编码为 LatLng 对象。您不是传递带有地址的对象,而是传递 latlng 对象,这是错误的。

更改此:

geocoder.geocode(latlng, function(results, status) {...

这样:

geocoder.geocode({address: "type your address here"}, function(results, status) ...

更新:如果您想要进行反向地理编码(从 latlng 获取地址),请查看此链接: http://code.google.com/apis/maps/documentation/javascript/services.html#ReverseGeocoding

关于javascript - 对 Google map 上的地理位置进行地理编码会出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7946527/

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