例子是寻找上海市奉贤区的200400-->紧急避难场所共33个;以及上海市奉贤区的090100-->综合医院16个。
具体程序如下:
#__author__='ZHENGT'
# -*- coding: utf-8 -*-
#http://lbs.amap.com/api/webservice/reference/search/#t5
#http://blog.csdn.net/niubingwei/article/details/50832585
#key=945ae54516979776ab3ee717012c24d4
#查找amap POI信息
import webbrowser
import os
import sys
import urllib
import json
import codecs
import string
from mako.template import Template
file_1 = "POI_Search.txt" #keep result into txt
file_2="POI_lnglat.js" #keep lnglat for MassMark
file_3="allCity.js" #城市列表
file_4="AMAP_JS.html" #HTML网页文件
# WEB查询地址,keyword为空,types=>变量,city=>变量
s_type ='200400' #AMAP分类
s_city_code = 310120 #奉贤区
s_city_zh="奉贤区" #中文 for center
each_page_rec=20 #每页记录数
amap = r'http://restapi.amap.com/v3/place/text?' \
r'&keyword=&types=%s&city=%s&citylimit=true&' \
r'&output=json&offset=20&page=1&key=945ae54516979776ab3ee717012c24d4&' \
r'extensions=base'
url = amap % (s_type, s_city_code) #代入变量值
# print url #显示观察URL数据
def Get_HTML(url): #<--获取POI信息-->
page = urllib.urlopen(url) #访问网页
data = page.readline() #读入数据
data_json = json.loads(data) #转换成python->字典
data_status=data_json['status'] #pois状态值
data_count = data_json['count'] # pois计数项
if data_status==0:
return -1 #未找到返回-1
else:
return data_count #返回数量值
def Parse_HTMl(url): #<--
page = urllib.urlopen(url) #问网页
data = page.readline() #读入数据
data_json = json.loads(data) #转换成python->字典
data_pois = data_json['pois'] #获取pois信息,list存储
for i_pois in data_pois: #遍历pois,字典存储
i_name=i_pois['name'] #name
i_address=i_pois['address'] #address
if len(i_address)==0: #<-处理没有地址情况
i_address="Not_Found"
i_location=i_pois['location'] #location
Write_to_Txt(i_name,i_address,i_location) #写入txt
# print i_name, "-->", "-->", i_location
def Write_to_Txt(name,address,location): #<--信息写入文件-->
f1=open(file_1,'a') #文件目标
str_line=name+";"+address+";"+location #字符
f1.write(str_line.encode('utf-8')) #字符写入文件,需要转码
f1.write("\n") #回车
f1.close() #关闭文件
def Txt_to_JS(): #<--将TXT转换成JS格式-->
#var pois= [{"lnglat": ["116.405285", "39.904989"], "name":"xxxx"},{}, …]
JS_POI=[]
f1 = open(file_1, 'r') #源txt文件
f2= open(file_2,'a') #目标JS文件
JS_Path=sys.path[0] #文件地址目录
lines=f1.readlines() #读取行
for line in lines: #循环行
if len(line)>0: #确保不是空行
cur_line=line.split(";") #分割
lnglat=cur_line[2].strip('\n').split(",")
lng=lnglat[0]
lat=lnglat[1]
lnglat_List=[lng,lat]
POI_name=cur_line[0]
List_Dict = {"lnglat": lnglat_List, "name": POI_name}
JS_POI.append(List_Dict) #字典存入List
str_POI=json.dumps(JS_POI,ensure_ascii=False)
str="var pois="+str_POI
f2.write(str)
f1.close()
f2.close()
return JS_Path #返回JS文件地址
def Get_Center(str_City): #<--返回城市的CENTER地址-->
f1=open(file_3,'r')
lines=f1.readlines()
f1.close()
data=lines[0]
data_line=data.split("=")
js_lines=json.loads(data_line[1],encoding='utf-8')
for js_dic in js_lines:
js_dic_name=js_dic['name']
js_name=js_dic_name.encode('utf-8')
if js_name==str_City:
js_lnglat=js_dic['lnglat']
lng=js_lnglat[0].encode('utf-8')
lat=js_lnglat[1].encode('utf-8')
lnglat=[float(lng),float(lat)]
# print lnglat
return lnglat
def Get_Amap(POI_Center,POI_Address): #<--处理网页-->
#HTML模版
url_MassMark = r'C:\Users\zhengt\Desktop\Study\Python\Python_Study_Proj\Sample\AMAP\amap_JS_MassMark.html'
tmple = Template(filename=url_MassMark, input_encoding='utf-8') #根据amap构建模板对象
str_html = tmple.render(adr=POI_Address,cent=POI_Center) #将location传递给mako的template
# print str_html
f1 = codecs.open(file_4,'w','utf-8') #另存为文件
f1.write(str_html) #写入信息
f1.close() #关闭文件
#主程序
if __name__ == '__main__':
if os.path.exists(file_1): #每次都存入新的文件
os.remove(file_1)
# 获取网页信息记录数
Total_Record_POI=Get_HTML(url) #返回计数项,-1为err
Total_Record=string.atoi(str(Total_Record_POI)) #uncode=>int
print Total_Record #显示总计录数
if Total_Record==0: #记录存在1条
url_amap=url
elif Total_Record>0:
if(Total_Record%each_page_rec)!=0: #总记录数无法整除
page_number=Total_Record/each_page_rec+1
else: #总记录数可以整除
page_number = Total_Record / each_page_rec
#retrive the records
for each_page in range(1,page_number+1):
print 'parsing page===>'+str(each_page)
if each_page ==1:
url_amap=url
else:
url_amap=url_amap.replace('page='+str(each_page-1),'page='+str(each_page))
# print url_amap
Parse_HTMl(url_amap) #处理网页内容
# 处理以地图形式显示点marker/MassMarkers
if Total_Record>=0:
if os.path.exists(file_2): #每次都存入新的文件
os.remove(file_2)
POI_Center=Get_Center(s_city_zh) #POI center
POI_Address=Txt_to_JS()
Get_Amap(POI_Center,POI_Address)
webbrowser.open(file_4)
sys.exit(0)
高德地图的HTML基本上引用高德网页的模版
<!doctype html>
<html lang="en" >
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
<style type="text/css">
html,body,.map{
width: 100%;
height: 100%;
margin: 0px;
}
</style>
<title>海量麻点图</title>
</head>
<body onload>
<div id="mapDiv" class="map" tabindex="0"></div>
<!--<script type="text/javascript" src = 'C:\Users\zhengt\Desktop\Study\Python\Python_Study_Proj\Sample\AMAP\POI_lnglat.js'></script>-->
<script type="text/javascript" src = '${adr}\POI_lnglat.js'></script>
<script type="text/javascript" src = 'http://webapi.amap.com/maps?v=1.3&key=945ae54516979776ab3ee717012c24d4'></script>
<script type="text/javascript">
var map = new AMap.Map('mapDiv', {
layers: [new AMap.TileLayer({
textIndex: 2
})],
zoom: 12,
center: ${cent}
});
var mass = new AMap.MassMarks(pois, {
url: 'http://webapi.amap.com/theme/v1.3/markers/n/mark_r.png',
anchor: new AMap.Pixel(3, 7),
size: new AMap.Size(10, 10),
opacity:1,
cursor:'pointer',
zIndex: 1
});
var marker = new AMap.Marker({
content:' ',
map:map
})
mass.on('mouseover',function(e){
marker.setPosition(e.data.lnglat);
marker.setLabel({content:e.data.name})
})
mass.setMap(map);
</script>
</body>
</html>