我想根据我在文本框中输入的位置在地图上获取圆圈。我到了这个地方而不是圈子。我调试了它,它总是转到应该显示错误对话框的else部分。但我也没有得到对话框。我检查了控制台,我得到错误:
InvalidValueError:setCenter:不是LatLng或LatLngLiteral:不是 对象
CODE:
<!DOCTYPE link href="https://maps/documentation/javascript/examples/default.css" rel="stylesheet" />
<html>
<head>
<meta charset="UTF-8" />
<title>Geolocation and Google Maps API</title>
<script src="http://maps.google.com/maps/api/js?sensor=true" type="text/javascript"></script>
<script>
function writeAddressName()
{
var geocoder = new google.maps.Geocoder();
var add = document.getElementById('address').value;
var myOptions =
{
zoom: 9,
center: add,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var mapObject = new google.maps.Map(document.getElementById("map"), myOptions);
geocoder.geocode(
{
'address': add
},
function (results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
var circle = new google.maps.Circle(
{
center: mapObject.setCenter(results[0].geometry.location),
radius: /*position.coords.accuracy*/16090 * 2,
map: mapObject,
fillColor: '#0000FF',
fillOpacity: 0.5,
strokeColor: '#0000FF',
strokeOpacity: 1.0
});
mapObject.fitBounds(circle.getBounds());
mapObject.setCenter(results[0].geometry.location);
var hint = new google.maps.Marker(
{
mapObject: mapObject,
position: results[0].geometry.location
});
}
else
{
alert('This code is not successful ' + status);
}
});
}
google.maps.event.addDomListener(window, 'load', writeAddressName);
</script>
<style type="text/css">
#map
{
height: 450px;
border: 5px solid #5E5454;
}
</style>
</head>
<body>
<h1>Basic example</h1>
<div id="map"></div>
<div id="panel">
Enter your region:
<input id="address" type="textbox">
<input type="button" value="Search" onclick="writeAddressName()">
</div>
</body>
</html>
我试图通过使用手段设置中心,但没有一个工作。任何形式的帮助将不胜感激。提前致谢
答案 0 :(得分:3)
您将Circle的中心设置为
center: mapObject.setCenter(results[0].geometry.location)
但.setCenter
的{{1}}方法不会返回mapObject
个对象,因此LatLng
未定义。
直接使用该位置:
center: