我在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());
}
地圖顯示就好了。
這是怎麼回事?