软测单独项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

100 lines
4.1 KiB

var map = new AMap.Map('container', {
resizeEnable: true, //是否监控地图容器尺寸变化
zoom: 13, //初始化地图层级
center: [105.728393, 34.560212] //初始化地图中心点
//center: [117.12783, 36.674013] //初始化地图中心点
//,mapStyle: 'amap://styles/954adf1b935afaab14d259c98ead7292' //设置地图的显示样式
});
map.plugin('AMap.Geolocation', function () {
geolocation = new AMap.Geolocation({
enableHighAccuracy: true,//是否使用高精度定位,默认:true
timeout: 10000, //超过10秒后停止定位,默认:无穷大
maximumAge: 0, //定位结果缓存0毫秒,默认:0
convert: true, //自动偏移坐标,偏移后的坐标为高德坐标,默认:true
showButton: true, //显示定位按钮,默认:true
buttonPosition: 'LB', //定位按钮停靠位置,默认:'LB',左下角
buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
showMarker: true, //定位成功后在定位到的位置显示点标记,默认:true
showCircle: false, //定位成功后用圆圈表示定位精度范围,默认:true
panToLocation: true, //定位成功后将定位到的位置作为地图中心点,默认:true
zoomToAccuracy: false, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
zoom:13,
markerOptions: {//自定义定位点样式,同Marker的Options
offset: new AMap.Pixel(-18, -36),
content: '<img src="https://a.amap.com/jsapi_demos/static/resource/img/user.png" style="width:36px;height:36px"/>'
},
circleOptions: {//定位精度圈的样式
strokeColor: '#0093FF',
noSelect: true,
strokeOpacity: 0.5,
strokeWeight: 1,
fillColor: '#02B0FF',
fillOpacity: 0.25
}
});
map.addControl(geolocation);
geolocation.getCurrentPosition(); //精准定位
// geolocation.getCityInfo(); //定位到城市
AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息
AMap.event.addListener(geolocation, 'error', onError); //返回定位出错信息
});
//解析定位结果
function onComplete(data) {
if (data.status == 1) {
console.log("定位成功啦");
$("#addressInput").val(data.formattedAddress);
sessionStorage.setItem("dingWei-lng", data.position.getLng());
sessionStorage.setItem("dingWei-lat", data.position.getLat());
}
var str = [];
str.push('经度:' + data.position.getLng());
str.push('纬度:' + data.position.getLat());
str.push('是否经过偏移:' + (data.isConverted ? '是' : '否'));
console.log(str.join('\n'));
$("#districtName").val(data.position.getLng() + "," + data.position.getLat());
$('#LON', window.parent.frames[0].document).val(data.position.getLng())// 赋值
$('#LAT', window.parent.frames[0].document).val(data.position.getLat())// 赋值
}
function onError() {
var str = [];
str.push('定位失败');
console.log(str.join('\n'));
}
var placeSearch = "";
var marker = null;
AMap.service(["AMap.PlaceSearch"], function () {
//构造地点查询类
placeSearch = new AMap.PlaceSearch({
map: map, // 展现结果的地图实例
autoFitView: true // 是否自动调整地图视野使绘制的 Marker点都处于视口的可见范围
});
//关键字查询
});
$("#search").click(function () {
placeSearch.search($("#poiname").val());
});
map.on("click", function (e) {
if (marker != null) {
map.remove(marker);
}
marker = new AMap.Marker({
position: new AMap.LngLat(e.lnglat.getLng(), e.lnglat.getLat()),
offset: new AMap.Pixel(-13, -30)
});
map.add(marker);
$("#districtName").val(e.lnglat.getLng() + "," + e.lnglat.getLat());
$('#LON', window.parent.frames[0].document).val(e.lnglat.getLng())// 赋值
$('#LAT', window.parent.frames[0].document).val(e.lnglat.getLat())// 赋值
});