发布:2023/12/7 15:55:49作者:大数据 来源:大数据 浏览次数:970
根据具体位置及地址获取经度和纬度
1 2 3 4 5 6 7 8 9 10 11 12 |
//根据地址获取经度和纬度 function getLngLat(location){ var res; $.ajax({url:"http://restapi.amap.com/v3/geocode/geo?key=此处替换成自己的高德key&s=rsv3&address="+location, async:false, success:function(result){ var arr= result.geocodes[0].location.split(","); res=new AMap.LngLat(arr[0],arr[1]); //console.log(res); }}); return res; } |
使用实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
var marker = new AMap.Marker({ map: map, icon: new AMap.Icon({ image: '/images/marker-green.png', //size: new AMap.Size(50, 50), imageSize:new AMap.Size(50,50) }), position:getLngLat('北京市朝阳区阜荣街10号'), title: 'xxx集团有限公司', offset: new AMap.Pixel(-13, -30), label: { offset: new AMap.Pixel(0, 0), direction: 'right', content: "" } }); marker.id = '162592';marker.on('click', showInfoM); marker.setMap(map); |
第二种办法利用高德提供的插件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
function getLocationXY(address) { //var map = new AMap.Map("container", { // resizeEnable: true //}); //加载地理编码插件 map.plugin(["AMap.Geocoder"], function () { //加载地理编码插件 geocoder = new AMap.Geocoder({ city: "", //城市设为北京,默认:“全国” }); //var marker = new AMap.Marker(); //address = '省市区' geocoder.getLocation(address, function (status, result) { if (status === 'complete' && result.geocodes.length) { var lnglat = result.geocodes[0].location //$("#Longitude").val(lnglat.lng); //$("#Latitude").val(lnglat.lat); //这里可直接使用lnglat或在此调用函数如:foo(lnglat) console.log("[",lnglat.lng,",",lnglat.lat+"]"); //marker.setPosition(lnglat); //map.add(marker); //map.setFitView(marker); } else { log.error('根据地址查询位置失败'); } }); }); } |
这种方法因为是异步访问,导致需要返回坐标位置时为空。
© Copyright 2014 - 2025 柏港建站平台 ejk5.com. 渝ICP备16000791号-4