Code for integration of Google Map Api and GOOGLE Search Api

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.