文章目录
高德地图js API实例
https://lbs.amap.com/api/javascript-api/example/marker/marker-anchor
1、实例化点标记
<div id='container' class="ta_map"></div>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.9&key=XXXX"></script>
var mlat = data.latitude;//纬度
var mlon = data.longitude;//经度
var lnglatXY = [mlon,mlat];
// 实例化点标记
function addMarker(lnglatXY) {
var map;
var lnglatXY = lnglatXY;
map = new AMap.Map('mapContainer', {
resizeEnable: true,
zoom:11,
center: lnglatXY
});
marker = new AMap.Marker({
icon: "http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
position: lnglatXY
});
marker.setMap(map);
map.setFitView();// 执行定位
return false;
}
2、高德获取当前定位
//高德获取当前定位
var map, geolocation;
//加载地图,调用浏览器定位服务
map = new AMap.Map('container', {
resizeEnable: true,
expandZoomRange:true,
zoom:10,
zooms:[3,10],
});
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: 'RB',
});
map.addControl(geolocation);
geolocation.getCurrentPosition();
AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息
AMap.event.addListener(geolocation, 'error', onError); //返回定位出错信息
});
//解析定位结果
function onComplete(data) {
$('#report_position').val(data.formattedAddress);
//根据地址显示位置
}
//解析定位错误信息
function onError(data) {
mui.toast('定位失败,请手动填写位置!');return;
}