今天先贴出搜索当前地图显示范围的数据搜索代码,批量搜索其实就是在这个基础上增加循环搜索即可。

这个代码是纯javascript写的,如果大范围搜索会存在效率问题和数据保存的问题,所以下一步的想法是通过google提供的非javascript的接口,写一个程序这搜索大的数据。

 

代码
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2  <html xmlns="http://www.w3.org/1999/xhtml">
3  <head>
4  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
5  <title>Google 本地搜索</title>
6  <style type="text/css" media="screen">
7 #mapCanvas {
8 border: 1px solid #979797;
9 height: 512px;
10 width: 512px;
11 float: left;
12  }
13 #mapSearch {
14 position: relative;
15 top: 0px;
16 left: 0px;
17 height: 300px;
18 float: left;
19 width: 300px;
20 margin-left: 10px;
21 }
22 #resultsCanvas {
23 top: 0px;
24 left: 0px;
25 width: 500px;
26 margin-left: 10px;
27 clear: both;
28 float: left;
29 }
30 </style>
31 <!--这里引用google ajax api-->
32 <script src="http://www.google.com/jsapi?key=ABQIAAAA1XbMiDxx_BTCY2_FkPh06RRaGTYH6UMl8mADNa0YKuWNNa8VNxQEerTAUcfkyrr6OwBovxn7TDAH5Q"></script>
33 <script type="text/javascript">
34 google.load('maps' , '2');
35 google.load('search' , '1');
36
37 var gmap;
38 var searcher;
39
40 //定义开始函数
41 function OnLoad() {
42
43 var mapCanvas = document.getElementById("mapCanvas");
44 gmap = new google.maps.Map2(mapCanvas);//建立地图控件
45
46 // ...and add a couple of controls.
47 gmap.addControl(new google.maps.SmallMapControl()); // Add a small map control
48 gmap.addControl(new google.maps.MapTypeControl()); // Add the map type control
49 // Set the map's center point and finish!
50 gmap.setCenter(new google.maps.LatLng(39.9081726 ,116.397949), 11);
51
52 searcher = new google.search.LocalSearch(); // 新建一个本地搜索
53 searcher.setCenterPoint(gmap); // 绑定搜索到地图
54
55 searcher.setResultSetSize(google.search.Search.LARGE_RESULTSET);// 定义每次显示的最大数,不过最大也只有8条
56
57 searcher.setSearchCompleteCallback(this,searchComplete);// 定义搜索完成后的回调函数
58 }
59
60 //搜索完成后函数,一次显示32条记录就是在这里处理的
61 function searchComplete() {
62 // Check that we got results
63 var message = document.getElementById("message");
64 if (searcher.results && searcher.results.length > 0) {
65 for (var i = 0; i < searcher.results.length; i++) {
66 var result = searcher.results[i];
67 // 显示搜索结果
68 message.value += result.titleNoFormatting + "," + result.lng + "," + result.lat + "\n";
69 var markerLatLng = new google.maps.LatLng(parseFloat(result.lat),
70 parseFloat(result.lng));
71 var marker = new google.maps.Marker(markerLatLng); // Create the marker
72
73 // Bind information for the infoWindow aka the map marker popup
74 marker.bindInfoWindow(result.html.cloneNode(true));
75 result.marker = marker; // bind the marker to the result
76 gmap.addOverlay(marker); // add the marker to the map
77 }
78 var cursor = searcher.cursor;
79 if(cursor.currentPageIndex < cursor.pages.length )//当前页数和总页数比较
80 searcher.gotoPage(cursor.currentPageIndex + 1);//搜索下一页
81 }
82 }
83 function clickSearch() {
84 document.getElementById("message").innerHTML = '';
85 var key = document.getElementById("txtKey").value;
86 searcher.execute(key);
87 }
88
89 google.setOnLoadCallback(OnLoad);
90 </script>
91 </head>
92 <body style="font-family: Arial; border: 0 none;">
93 <div id="mapCanvas"> Loading... </div>
94 <div id="resultsCanvas">
95 <table border="1">
96
97 </table>
98 <label>
99 <label>搜索关键字</label><input name="txtKey" id="txtKey" type="text" value="书店" />
100 <input type="submit" name="sss" id="sss" value="搜索当前范围" onclick="javascript:clickSearch();"/>
101 <input type="submit" name="sss" id="sss" value="暂停" onclick="javascript:stopSearch();"/>
102 </label>
103 </div>
104 <div id="mapSearch">
105 搜索结果
106 <textarea id="message" name="sss" cols="40" rows="20"></textarea>
107 </div>
108 </body>
109 </html>
110
posted on 2010-05-27 10:05  liongis  阅读(1801)  评论(0编辑  收藏  举报