<div id="allmap" style="height:300px"></div>
<input type="hidden" id="lng" name="lng" value="" />
<input type="hidden" id="lat" name="lat" value="" />
<script type="text/javascript">
var map, geolocation;
//加载地图,调用浏览器定位服务
map = new AMap.Map('allmap', {
resizeEnable: true,
view: new AMap.View2D({
resizeEnable: true,
zoom:50//地图显示的缩放级别
}),
keyboardEnable:false
});
map.plugin(["AMap.ToolBar"],function(){
toolBar = new AMap.ToolBar();
map.addControl(toolBar);
toolBar.show();
toolBar.hideRuler();
toolBar.hideDirection();
});
map.plugin(["AMap.MapType"],function(){
//地图类型切换
mapType= new AMap.MapType({defaultType:0,showRoad:true});
map.addControl(mapType);
});
map.plugin('AMap.Geolocation', function() {
geolocation = new AMap.Geolocation({
enableHighAccuracy: true,//是否使用高精度定位,默认:true
timeout: 10000, //超过10秒后停止定位,默认:无穷大
buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
zoomToAccuracy: true, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
buttonPosition:'LB'
});
map.addControl(geolocation);
geolocation.getCurrentPosition();
AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息
AMap.event.addListener(geolocation, 'error', onError); //返回定位出错信息
});
//解析定位结果
function onComplete(data) {
var lon = data.position.getLng();
var lat = data.position.getLat();
new AMap.Marker({
map: map,
position: [lon,lat],
icon: new AMap.Icon({
size: new AMap.Size(19, 33), //图标大小
image: "/image/mark_bs.png",
})
});
map.setZoomAndCenter(20, [lon,lat]);
var clickEventListener=AMap.event.addListener(map,'click',function(e){
map.clearMap();
new AMap.Marker({
position: e.lnglat,
map: map
});
document.getElementById("lng").value=e.lnglat.getLng();
document.getElementById("lat").value=e.lnglat.getLat();
});
}
//解析定位错误信息
function onError(data) {
document.getElementById('tip').innerHTML = '定位失败';
}
</script>