<script src="
https://api.map.baidu.com/api?v=2.0&ak=百度ak&s=1" type="text/javascript"></script>
<script>
//触发获取
function getLocation() {
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(translatePoint,locationError);
} else {
//alert('浏览器不支持地理定位。');
}
}
//安卓等H5可用
function translatePoint(position){
var currentLat = position.coords.latitude;
var currentLon = position.coords.longitude;
var gpsPoint = new BMap.Point(currentLon, currentLat);
var myGeo = new BMap.Geocoder();
myGeo.getLocation(gpsPoint, function (result) {
var city = result.addressComponents.city;
$('#chengshi').html("尊敬的"+city+"顾客,您好!");
});
}
//苹果不支持h5拉取地址 额外处理
function locationError(){
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function(r){
if(this.getStatus()== BMAP_STATUS_SUCCESS){
var currentLat = r.point.lat;
var currentLon = r.point.lng;
var gpsPoint = new BMap.Point(currentLon, currentLat);
var myGeo = new BMap.Geocoder();
myGeo.getLocation(gpsPoint, function (result) {
var city = result.addressComponents.city;
$('#chengshi').html("尊敬的"+city+"顾客,您好!");
});
}
});
}
</script>