Google 地图未显示用户位置
Google Map not showing users location
我有一张 google 地图,我试图显示用户的位置,如果我添加纬度和经度它很好,但是当我尝试动态获取位置时它不显示地图。
这个有效
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.LatLng(-33.863276, 151.207977),
zoom: 12
});
但这不是
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.(position.coords.latitude, position.coords.longitude),
zoom: 13
});
您需要访问 navigator.geolocation
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), { zoom: 12 });
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
map.setCenter(pos);
});
}
}
我有一张 google 地图,我试图显示用户的位置,如果我添加纬度和经度它很好,但是当我尝试动态获取位置时它不显示地图。
这个有效
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.LatLng(-33.863276, 151.207977),
zoom: 12
});
但这不是
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.(position.coords.latitude, position.coords.longitude),
zoom: 13
});
您需要访问 navigator.geolocation
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), { zoom: 12 });
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
map.setCenter(pos);
});
}
}