我正在使用谷歌地图api v3。这是我的代码
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
<script src="https://maps.googleapis.com/maps/api/js?
v=3.exp&sensor=false&libraries=places"></script>
<script>
var geocoder;
var map;
function initialize() {
var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input);
//var latlng = new google.maps.LatLng(18.52043030000, 73.85674369999);
var mapOptions = {
zoom: 15,
//center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById('googleMap'), mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
function codeAddress() {
var address = document.getElementById('searchTextField').value;
geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'searchTextField': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
alert(latitude);
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);
}
});
}
</script>
我在wordpress模板文件中做了这一切。我正在使用自动填充位置显示建议。我希望当用户从建议列表中选择一个位置时,地图应该导航到该位置。它没有进入if条件,即if(status == google.maps.GeocoderStatus.OK){
这是html代码
<input id="searchTextField" type="textbox">
<input type="button" value="Geocode" onclick="codeAddress()">
答案 0 :(得分:1)
如果您运行此代码,您会看到错误消息:
InvalidValueError: unknown property searchTextField
Geocoder接受'地址'作为其他属性,但没有'searchTextField' 所以只需改变:
geocoder.geocode( { 'searchTextField': address}, function(results, status) {
为:
geocoder.geocode( { 'address': address}, function(results, status) {