Integrate the google map api with the GOOGLE Search API. I wrote a class in object-oriented mode, passed a latitude and longitude, and automatically obtained nearby information through GOOGLE LOCAL SEARCH. Such as restaurants and scenic spots, which are marked on the map in turn and can be displayed in any container.
The following is the source code:
Copy codeThe Code is as follows:
/*
* Author: karry
* Version: 1.0
* Time: 2008-12-01
* KMapSearch class
* Combines google map with LocalSearch. You only need to input the MAP/latitude/longitude value to obtain the local search content near the latitude and longitude, mark the content on the MAP, and display the search result in the specified container.
*/
(Function (){
Var markers = new Array ();
Var KMapSearch = window. KMapSearch = function (map _, opts _){
This. opts = {
Container: opts _. container | "divSearchResult ",
KeyWord: opts _. keyWord | "restaurant ",
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/> address:" + 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.exe cute = function (latLng ){
If (latLng ){
This. gLocalSearch. setCenterPoint (latLng );
}
This.gLocalSearch.exe cute (this. opts. keyWord );
}
})();
Usage:
Copy codeThe Code is as follows:
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: "restaurant "});
MapSearch. clearAll ();
MapSearch.exe cute ();
Click here to view the demo: integrate local search for latitude and longitude queries