显示您要在网址中分享您的位置,但不会显示地图。所以我想知道哪里出错了?但它显示在IE和Firefox上,但没有在safari和chrome上显示
$(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 :(得分:0)
这是范围的问题。我修复了它(jsfiddle here: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。