色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

Google Map Api和GOOGLE Search Api整合實現代碼

 

       將GOOGLE MAP API 和 GOOGLE Search API 進行整合,我用面向對象的方式寫了一個類,通過傳一個經緯度進去,自動通過GOOGLE LOCAL SEARCH獲取附近的相關信息。比如餐廳、景點等,反過來標到地圖上,并可在任意容器內顯示。
下面是源碼:
復制代碼 代碼如下:
/*
*Author:karry
*Version:1.0
*Time:2008-12-01
*KMapSearch 類
*把GOOGLE MAP 和LocalSearch結合。只需要傳入MAP/經緯度值,就可以把該經緯度附近的相關本地搜索內容取出來,在地圖上標注出來,并可以在指定容器顯示搜索結果
*/

(function() {
var markers= new Array();
var KMapSearch=window.KMapSearch= function(map_, opts_) {
this.opts = {
container:opts_.container || "divSearchResult",
keyWord:opts_.keyWord || "餐廳",
latlng: opts_.latlng || new GLatLng(31, 121),
autoClear:opts_.autoClear || true,
icon:opts_.icon || new GIcon(G_DEFAULT_ICON)
};
this.map = map_;
this.gLocalSearch = new google.search.LocalSearch();
this.gLocalSearch.setCenterPoint(this.opts.latlng);
this.gLocalSearch.setResultSetSize(GSearch.LARGE_RESULTSET);
this.gLocalSearch.setSearchCompleteCallback(this, function() {
if (this.gLocalSearch.results) {
var savedResults = document.getElementById(this.opts.container);
if (this.opts.autoClear) {
savedResults.innerHTML = "";
}
for (var i = 0; i < this.gLocalSearch.results.length; i++) {
savedResults.appendChild(this.getResult(this.gLocalSearch.results[i]));
}
}
});
}
KMapSearch.prototype.getResult = function(result) {
var container = document.createElement("div");
container.className = "list";
var myRadom =(new Date()).getTime().toString()+Math.floor(Math.random()*10000);
container.id=myRadom;
container.innerHTML = result.title + "<br />地址:" + result.streetAddress;
this.createMarker(new GLatLng(result.lat, result.lng), result.html,myRadom);
return container;
}
KMapSearch.prototype.createMarker = function(latLng, content)
{
var marker = new GMarker(latLng, {icon:this.opts.icon,title:this.opts.title});
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml(content);
});
markers.push(marker);
map.addOverlay(marker);
}
KMapSearch.prototype.clearAll = function() {
for (var i = 0; i < markers.length; i++) {
this.map.removeOverlay(markers[i]);
}
markers.length = 0;
}
KMapSearch.prototype.execute = function(latLng) {
if (latLng) {
this.gLocalSearch.setCenterPoint(latLng);
}
this.gLocalSearch.execute(this.opts.keyWord);
}
})();

使用方法:
復制代碼 代碼如下:
var myIcon = new GIcon(G_DEFAULT_ICON);
myIcon.image = "canting.png";
myIcon.iconSize = new GSize(16, 20);
myIcon.iconAnchor = new GPoint(8, 20);
myIcon.shadow = "";
var mapSearch = new KMapSearch(map, {container:"cantingContainer",latlng:initPt,icon:myIcon,keyWord:"餐廳"});
mapSearch.clearAll();
mapSearch.execute();

點擊這里查看演示示例:經緯度查詢整合本地搜索

JavaScript技術Google Map Api和GOOGLE Search Api整合實現代碼,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 阿瓦提县| 深州市| 高雄市| 屏山县| 两当县| 宜宾县| 麻栗坡县| 柞水县| 堆龙德庆县| 康定县| 沙湾县| 象州县| 云梦县| 宿松县| 黄平县| 平度市| 平乡县| 晋江市| 亚东县| 吕梁市| 齐河县| 诸城市| 民乐县| 安平县| 桃园县| 墨玉县| 宁强县| 华阴市| 眉山市| 西贡区| 攀枝花市| 长葛市| 安吉县| 乌鲁木齐县| 陇西县| 本溪市| 株洲市| 两当县| 当阳市| 习水县| 海口市|