Google map API usage (20)-implement the Google map local search box (below)

Source: Internet
Author: User

2. Use the custom JS and gclientgeocoder classes in the Google map API

In fact, the Google map API provides a gclientgeocoder class to implement address decoding, that is, to convert place names and coordinates. This is to submit the place names from the client to find the database and return the coordinates.

Let's take a look at the following code:

VaR rpoint;

VaR map = new gmap2 (document. getelementbyid ("gmapcontainer "));

VaR geocoder = new gclientgeocoder ();

Geocoder. getlatlng (STR, function (rpoint ){

// Alert (STR );

If (! Rpoint ){

Alert (STR + "unable to parse address ");

}

Else {

// Alert (rpoint );

Map. setcenter (rpoint, 15 );

VaR marker = new gmarker (rpoint );

Map. addoverlay (Marker );

Marker. openinfowindowhtml ("Welcome to view" + STR + "map ");

Map. enabledoubleclickzoom ();

Map. enablescrollwheelzoom ();

Map. enablecontinuouszoom ();

Map. addcontrol (New glargemapcontrol ())

Map. addcontrol (New goverviewmapcontrol ());

Map. addcontrol (New gscalecontrol ());

Map. addcontrol (New gmaptypecontrol ());

New gkeyboardhandler (MAP );

// Map. enablegooglebar ();

}

});

From the code above, we can see that geocoder is an instance of the gclientgeocoder class.

Geocoder. getlatlng (STR, function );

To obtain the geographical coordinate value corresponding to the place name STR, and then display the map through the map object of the gmap2. From this we can see that using a custom method requires two Google map queries on the server. Of course, in terms of efficiency, it is not as good as googlebar or googlebar, but in some occasions, it is still necessary.

Example:Myapp2.html.

(End of this series)

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.