google.load('maps', '2'); //载入maps API 2.*库
google.load('search', '1'); //载入search API 1.*库
var gmap; //全局变量
var searcher; //全局变量
//定义开始函数
function OnLoad() {
var mapCanvas = document.getElementById("mapCanvas"); //获取用于显示地图的Div
gmap = new google.maps.Map2(mapCanvas); //绑定用于显示地图的Div
gmap.addControl(new google.maps.SmallMapControl()); // 添加控制地图大小的控件
gmap.addControl(new google.maps.MapTypeControl()); // 添加切换地图模式的控件
gmap.setCenter(new google.maps.LatLng(26.08, 119.28), 11); //默认地图中心定位到福州
//---------------自定义标记图片、背景图片和背影------------------------------------------------------------------------------
// var icon = new GIcon();
// icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
// icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
// icon.iconSize = new GSize(12,20);
// icon.shadowSize = new GSize(22,20);
// icon.iconAnchor = new GPoint(6,20);
// icon.infoWindowAnchor = new GPoint(5,1);
GEvent.addListener(gmap, "moveend", function() { //添加事件监听器,
clickSearch();
});
searcher = new google.search.LocalSearch(); // 实例化一个本地搜索对象
searcher.setCenterPoint(gmap); // 绑定搜索对象到地图
searcher.setResultSetSize(google.search.Search.LARGE_RESULTSET); //定义每次显示的最大数,不过最大也只有8条
searcher.setSearchCompleteCallback(this, searchComplete); // 定义搜索完成后的回调函数
}
//搜索完成后函数,一次显示32条记录就是在这里处理的
function searchComplete() {
var message = document.getElementById("message"); //获取用于存储结果的textarea
if (searcher.results && searcher.results.length > 0) { //验证是否采集到数据
for (var i = 0; i < searcher.results.length; i++) { //遍历采集结果
var result = searcher.results[i];
message.value += "115" + "," + result.titleNoFormatting + "," + result.title + "," + result.lng + "," + result.lat +","+ result.streetAddress +","+result.url+ "\n"; //提取结构集中属性并赋值给textarea
var markerLatLng = new google.maps.LatLng(parseFloat(result.lat), parseFloat(result.lng)); //定义标记的地理位置
var marker = new google.maps.Marker(markerLatLng); //创建标记
marker.bindInfoWindow(result.html.cloneNode(true)); //绑定弹出的infowindow的html信息
//result.marker = marker; //绑定标记到结果集,无用
gmap.addOverlay(marker); //把标记添加到地图
}
var cursor = searcher.cursor; //定义搜索游标
if (cursor.currentPageIndex < cursor.pages.length) //当前页数和总页数比较
searcher.gotoPage(cursor.currentPageIndex + 1); //搜索下一页
}
}
google.setOnLoadCallback(OnLoad);
//搜索函数
function clickSearch() {
var key = document.getElementById("TxtTargetData").value;
searcher.execute(key);
}
//清除结果函数
function ClearResult() {
OnLoad();
document.getElementById("message").value = '';
}