帖子
帖子
用户
博客
课程
显示全部楼层
56
帖子
0
勋章
1177
Y币

按关键字搜索地图,获得排序的周边商家地址列表

[复制链接]
发表于 2015-7-24 00:16:37
///按关键字搜索地图,获得周边商家地址列表
/// queryKey: 查询关键词
/// location: 中心点:"lat,lon" 对应的值
/// mapContainerId: html 页面内的容器Id
function map_getPlaceList(baiduLocation, queryKey, location, mapContainerId, page) {
if(!baiduLocation) {
  api.alert({msg:'没有开启百度地图模块!'});
  return;
}
    var query = queryKey;
    var location = location;
    var ak = "你的百度AK";
    var radius = 1000*200;
    //var region = location;
    api.ajax({
        url: 'http://api.map**.**/place/v2/search?scope=2&query='+query+'&location='+location+'&output=json&page_size=20&page_num='+page+'&radius='+radius+'&ak='+ak+'',
        method: 'get',
        timeout: 30,
        dataType: 'json',
        returnAll:false,
        data:{}
    },function(ret,err) {
        if (ret) {
            var str = ret.results;
            var html = '';
            var $placeList = $api.byId(mapContainerId);            
            if(ret.total>=0) {
             ///利用jsonSort对象 对获得的商家信息json数据按距离远近排序, 近的排前
             var jsonAryObj = new jsonSort(str, "detail_info.distance", "up") //建立对象
             jsonAryObj.sort();
                for(var i=0; i<str.length;i++) {
                    if(str.address){
                        var address = str.address;
                    }else{
                        var address = '';
                    }
                    var distance = str.detail_info.distance/1000;//将m转换为km
                    html += '<div>';
                    html += '<p>';
                    html += str.name;
                    html += '距离:'+distance.toFixed(2)+' km';
                    html += '</p>';
                    html += '<p>'+address+'</p>';
                    html += '</div>';
                    html += '<div class="h1"></div>';                    
               }
               $api.append($placeList,html);
               api.hideProgress();
               maplist_loaded = true;
            }
            else {
             maplist_loaded = false;
            }
        }
        else {
         maplist_loaded = false;
         }
     });
}


/// jsonSort.js
var jsonsort_temp = {
            init:function (arry, parm, sortby) {
                this.obj = arry
                this.parm = parm
                this.b = sortby
            },

            sort:function () {
                var $this = this
                var down = function (x, y) {
                    return (eval("x." + $this.parm) > eval("y." + $this.parm)) ? -1 : 1
                }//通过eval对json对象的键值传参
                var up = function (x, y) {
                    return (eval("x." + $this.parm) < eval("y." + $this.parm)) ? -1 : 1
                }
                if (this.b == "down") {
                    this.obj.sort(down)
                }
                else {
                    this.obj.sort(up)
                }

            },//排序

            prin:function () {
                var $text = "<div>"
                $.each(this.obj, function (key, value) {
                    var $div = "<div>"
                    $.each(value, function (key, value) {
                        $div += "<span>" + key + ":</span>" + "<span>" + value + "</span>" + "         "
                    })
                    $div += "</div>"
                    $text = $text + $div
                })
                $text += "</div>"
                $("html body").html($text)
            }//遍历添加dom元素,添加dom
        }

function jsonSort() {
this.init.apply(this, arguments)
}
jsonSort.prototype = jsonsort_temp;

4
帖子
0
勋章
28
Y币
请教 您有这个的demo吗  能否发我邮箱一份 我的邮箱78384529**.**
56
帖子
0
勋章
1177
Y币
代码都在这里啊,是我app中的以部分,代码中baiduLocation参数就是你自己 api.require('baiduMap') 的返回值
4
帖子
0
勋章
332
Y币
非常有用,谢谢分享。
56
帖子
0
勋章
1177
Y币
清风师爷 发表于 2015-7-24 16:22
代码都在这里啊,是我app中的以部分,代码中baiduLocation参数就是你自己 api.require('baiduMap') 的返回 ...

搞错了,因使用百度的http,baiduLocation 参数不要,也不要引入 baiduMap模块。
0
帖子
0
勋章
59
Y币
谢谢分享!!!!!
您需要登录后才可以回帖 登录

本版积分规则