我爱模板网 > 建站教程 > 地图,GIS教程 >  高德地图根据经纬度列出附近的POI正文

高德地图根据经纬度列出附近的POI

我爱模板网在做一个app时,需要根据经纬度显示附近的写字楼、商家等POI,并且移动地图的时候,实时更新POI列表,效果图如下:

高德地图搜索POI

打开地图、获取经纬度、设置mark的代码就不写了,先看下一开始写的根据经纬度获取poi:
01//根据经纬度获取地址 搜索兴趣点,并且按照由远及近排序
02aMap.searchNearby({
03    lon: that.x,   //获取的经度
04    lat: that.y,   //获取的纬度
05    keyword: '学校|饭店|餐馆|写字楼|广场|路口|酒店|大厦|公园|宾馆|会所|科技园|工业园|洗浴|小区|图书馆|道路|网吧',
06    offset: that.pageSize,
07    page: that.pageNum,
08    sortrule: 0,
09    radius:100
10}, function(ret) {
11    if (ret.status) {
12        if (that.pageNum === 1) {
13            that.list = [];
14        }
15        //按距离重新排序
16        ret.pois.sort(function(a, b) {
17            return Math.abs(a.distance - b.distance);
18        })
19        ret.pois.map(function(item, index) {
20            that.list.push({
21                checked: that.pageNum === 1 && index === 0 ? true : false,
22                name: item.name,
23                text: item.address,
24                lon: item.lon,
25                lat: item.lat,
26            })
27            if (that.pageNum === 1 && index === 0) {
28                that.curObj = {
29                    name: item.name,
30                    text: item.address,
31                    lon: item.lon,
32                    lat: item.lat
33                };
34            }
35        })
36        if (ret.pois.length > 0) {
37            that.pageNum++;
38            that.hasMore = true;
39        } else {
40            that.hasMore = false;
41        }
42        if (!that.hasMore && that.pageNum > 1) {
43            // $.msg('没有更多了!');
44        }
45        if (typeof fn === 'function') fn();
46        that.$forceUpdate();
47        func.hideProgress();
48    }
49});
上面代码的keyword就是你要显示的范围,我爱模板网尽可能写全了,但是发现结果还是很少,而且不准确,如:明明我在“联投V中心”,列表上始终没看到这个写字楼。这是apicloud提供的方法,看来不行,于是又到高德地图官网找了,终于找到了,使用的是JS SDK的方法实现的,代码如下:
001// 高德地图获取附近POI
002getPoi:function(location,page,offset,keywords,fn,fn2){
003    //key密钥
004    var key   = '这是key密钥',
005    //坐标转换(这里可要可不要,如果你获取坐标是高德,使用搜索poi也是高德,就没必要转换,但是是百度坐标的话,就将下面的coordsys字段的值改为百度即可,其他同理)
007    coordsys = 'autonavi'//原坐标系,可选值:gps;mapbar;baidu;autonavi(不进行转换) 默认autonavi
008    //获取附近POI
010    location   = location,
011    page        = page || 1,
012    offset     = offset || 20,
013    keywords   = keywords || '',
014    extensions = 'all';
015    //判断
016    if(location){
017        //发起ajax
018        api.ajax({
019            url: zbUrl+'?locations='+location+'&coordsys='+coordsys+'&output=json&key='+key,
020            method: 'get',
021            timeout:30,
022            data: {},
023        },function(ret,err)
024        {
025            console.log('************************ 请求开始 start ************************');
026            console.log('接口描述:' + '坐标转换');
027            console.log('请求URL:' + zbUrl);
028            console.log('接口返回:' + JSON.stringify(ret));
029            if (ret) { //请求服务器成功
030                //发起ajax
031                api.ajax({
032                    url: url,
033                    method: 'get',
034                    timeout:30,
035                    data: {
036                        body : {
037                            key       : key,
038                            location  : ret.locations,
039                            page       : page,
040                            offset    : offset,
041                            keywords  : keywords,
042                            extensions: extensions
043                        }
044                    },
045                },function(ret,err)
046                {
047                    console.log('************************ 请求开始 start ************************');
048                    console.log('接口描述:' + '高德地图附近POI');
049                    console.log('请求URL:' + url);
050                    console.log('请求参数:' + JSON.stringify({
051                        key       : key,
052                        location  : location,
053                        page       : page,
054                        offset    : offset,
055                        keywords  : keywords,
056                        extensions: extensions
057                    }));
058                    console.log('接口返回:' + JSON.stringify(ret));
059 
060                    if (ret) { //请求服务器成功
061                        //正常回调
062                        if(typeof fn == 'function') {fn(ret);}
063                    } else { //请求服务器失败
064                        //错误回调
065                        if(typeof fn2 == 'function') {fn2(ret);}
066                        if(err.code == 1){
067                            api.toast({
068                                msg: '当前网络较差',
069                                duration: 3000,
070                                location: 'middle'
071                            });
072                        }
073                        console.log('错误信息:' + JSON.stringify(err));
074                    }
075                    console.log('************************ 请求结束 end ************************');
076                });
077            }
078            console.log('************************ 请求结束 end ************************');
079        });
080    }else{
081        func.msg('经纬不存在')
082    }
083},
084 
085//使用
086func.getPoi(that.x+','+that.y,that.pageNum,that.pageSize,that.searchKey,function(res){
087    if (that.pageNum === 1) {
088        that.list = [];
089    }
090    res.pois.map(function(item, index) {
091        that.list.push({
092            checked: that.pageNum === 1 && index === 0 ? true : false,
093            name: item.name,
094            text: item.address,
095            lon: item.lon,
096            lat: item.lat,
097        })
098        if (that.pageNum === 1 && index === 0) {
099            that.curObj = {
100                name: item.name,
101                text: item.address,
102                lon: item.lon,
103                lat: item.lat
104            };
105        }
106    })
107    if (res.pois.length > 0) {
108        that.pageNum++;
109        that.hasMore = true;
110    } else {
111        that.hasMore = false;
112    }
113    if (!that.hasMore && that.pageNum > 1) {
114        // $.msg('没有更多了!');
115    }
116    if (typeof fn === 'function') fn();
117    that.$forceUpdate();
118    func.hideProgress();
119});
至此完成。我爱模板网承接各种混合app、小程序、公众号、网站开发,如果希望合作,可以联系QQ 543031222。

部分素材资源来源网站,本站提供免费下载,如有侵权请联系站长马上删除!
上一篇:JS中高德、百度地图坐标互相转换 下一篇:Cesium Cartesian3笛卡尔坐标系详解和转换
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
选择头像:
最新评论

猜你喜欢