我在Google Maps JavaScript API和HTML5地理位置方面遇到了一个相当奇怪的问题。谷歌地图javascript API + HTML5地理位置怪癖
我有以下执行的onload:
var geocoder;
var map;
var latlng;
function initialize() {
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(position){
latlng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
});
}else{
latlng = new google.maps.LatLng(geoip_latitude(),geoip_longitude());
}
geocoder = new google.maps.Geocoder();
var myOptions = {
zoom: 8,
center: latlng,
streetViewControl: false,
mapTypeId: google.maps.MapTypeId.HYBRID
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
var latLong = String(event.latLng).replace('(', '');
latLong = latLong.replace(')', '');
$('#latlng').attr('value', latLong);
});
function placeMarker(location) {
if(undefined != initialize.marker){
initialize.marker.setMap(null);
}
initialize.marker = new google.maps.Marker({
position: location,
map: map
});
initialize.marker.setMap(map);
map.setCenter(location);
}
}
现在,这是什么应该做的是浏览器是否支持地理定位,得到了纬度+长,创建地图,并显示为中心在地图上LAT +长。
,如果我使用geoip_功能(的MaxMind),但地理位置有一个奇怪的问题,工作得很好:
如果代码运行的是,在谷歌地图显示显示为没有一个灰色框地图和没有控制。
如果我添加一个alert();立即之后
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(position){
latlng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
});
}else{
latlng = new google.maps.LatLng(geoip_latitude(),geoip_longitude());
}
地图显示就好了。
这是怎么回事?