map.setCenter()函数工作不正常 [英] map.setCenter() function is not working properly
本文介绍了map.setCenter()函数工作不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这里是代码...
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var address = "new delhi";
geocoder.geocode( { 'address': 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);
alert(longitude);
map.setCenter(new GLatLng(latitude, longitude));
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
var latlng = new google.maps.LatLng(latitude, longitude);
var mapOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}
});
google.maps.event.addDomListener(window, 'load', initialize);
}
</script>
推荐答案
您需要像这样设置中心。 。
You'll need to set center like this...
map.setCenter(new google.maps.LatLng(latitude, longitude));
另外,您在初始化map之前设置中心,因此抛出错误。
Also you are setting center before initializing map and so its throwing an error.
这里是更新的JS。
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var address = "new delhi";
geocoder.geocode({
'address': 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);
alert(longitude);
var latlng = new google.maps.LatLng(latitude, longitude);
var mapOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var latlng = new google.maps.LatLng(latitude, longitude);
map.setCenter(latlng);
var marker = new google.maps.Marker({
map: map,
position: latlng,
title: 'Hello World!'
});
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
这篇关于map.setCenter()函数工作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文