|
///按关键字搜索地图,获得周边商家地址列表
/// 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;
|
|