2014-04-09 40 views
0

它顯示你想在url中分享你的位置,但不會顯示地圖。所以我想知道哪裏可以出問題?但它顯示在IE和Firefox,但沒有出現在Safari和Chrome谷歌地圖API:無法顯示地圖

$(document).ready(function(){ 
initialize(); 
$("#btInit").click(function(){ 

    navigator.geolocation.getCurrentPosition(onSuccess); 
}); //call this function when the user click 
}); 

var map; 

// MAP

function initialize() { 
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
var mapOptions = { 
zoom: 17 
}; 
} 

//地理位置

function onSuccess(position){ 
    var myLat = position.coords.latitude; 
    var myLong = position.coords.longitude; 
    var latLng = new google.maps.LatLng(myLat, myLong); 

    map.setCenter(latLng); 


google.maps.event.addDomListener(document, initialize); 
+0

我應該不得不關注此鏈接 http://www.paulund.co.uk/how-to-use-geolocation-api-with-google-maps –

回答

0

這是一個與範圍的問題。我固定它,像這樣(的jsfiddle這裏:http://jsfiddle.net/gjacobs/D9ug8/1/):

jQuery(document).ready(function() { 
    // Ask for permission 
    navigator.geolocation.getCurrentPosition(function(position){ 
     var myLat = position.coords.latitude; 
     var myLong = position.coords.longitude; 
     var latLng = new google.maps.LatLng(myLat, myLong); 
     map.setCenter(latLng); 
    }); 

    var map; 
    var centerPosition = new google.maps.LatLng(40.747688, -74.004142); 
    var options = { 
     zoom: 16, 
     center: centerPosition, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    map = new google.maps.Map($('#map')[0], options); 
}); 

但也有其他的選擇,那給你清潔的JavaScript。

+0

它顯示在Firefox和ie上,但它沒有顯示在鉻和Safari瀏覽器 – noooooooo