HTML5 HTML5 调用百度地图API地理定位实例
完整代码如下图,js代码在下面可直接复制到 script
1、引用百度地图的js
2、复制下面的js代码就可以预览效果
window.onload=function() {
if(navigator.geolocation) {
document.getElementById("status").innerHTML="HTML5 Geolocation is supported in your browser.";
// 百度地图API功能
varmap=newBMap.Map("container");
varpoint=newBMap.Point(116.331398,39.897445);
map.centerAndZoom(point,12);
vargeolocation=newBMap.Geolocation();
geolocation.getCurrentPosition(function(r){
if(this.getStatus() == BMAP_STATUS_SUCCESS){
varmk=newBMap.Marker(r.point);
map.addOverlay(mk);
map.panTo(r.point);
alert('您的位置:'+r.point.lng+','+r.point.lat);
}
else {
alert('failed'+this.getStatus());
}
},{enableHighAccuracy: true})
}
};