百度地图定位到当前城市:
map = new BMap.Map("map", {});// 创建Map实例
var myCity = new BMap.LocalCity();
myCity.get(myFun);
//定位到当前城市
function myFun(result) {
var cityName = result.name;
map.setCenter(cityName);
}
Google地图定位当前位置(https网址可以正常弹出定位提示框,http不行)
//初始化地图
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 8,
fullscreenControlOptions: {
//将全屏控件放到地图下方,默认在右上角
position: google.maps.ControlPosition.BOTTOM_RIGHT
},
});
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
map.setCenter(pos);
}, function () {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
// layer.alert("Error: The Geolocation service failed.")
handleLocationError(false, infoWindow, map.getCenter());
}
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
infoWindow.open(map);
}