Google Map API and Google Search API integration to implement code _JAVASCRIPT skills

Source: Internet
Author: User

By integrating the Google MAP API with the Google search API, I wrote a class in an object-oriented way, passing through a latitude and longitude to automatically get the relevant information from Google Local Search. such as restaurants, attractions, and so on, in turn marked on the map, and can be displayed in any container.
The following is the source code:

Copy Code code as follows:

/*
*author:karry
*version:1.0
*time:2008-12-01
*kmapsearch class
* Combine Google MAP with Localsearch. Only need to pass in the Map\ latitude value, you can take the latitude and longitude near the relevant local search content out, on the map annotated, and can display the search results 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.execute = function (latlng) {
if (LATLNG) {
This.gLocalSearch.setCenterPoint (LATLNG);
}
This.gLocalSearch.execute (This.opts.keyWord);
}
})();

How to use:
Copy Code code 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.execute ();

Click here to see the demo example: a latitude and longitude query consolidated local Search

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.