"Baidu Map API" When address resolution fails, how to call the search method to find an address

Source: Internet
Author: User

Original: "Baidu Map API" When address resolution fails, how to call the search method to find an address

A friend asked me, what should I do when the address resolution fails? For example, he wanted to search for "Nanning".


First, we need to understand the principle of address resolution :

use Geocoder for address resolution, such as "10 Street, Haidian District, Beijing", when the system matches this address, GetPoint will return a coordinate point.
The callback function is needed here.
varMygeo= NewBmap.geocoder ();//displays address resolution results on the map and adjusts the map view
Mygeo.getpoint ("Shang 10 Jie, Haidian District, Peking City", function(point) {
if(point) {
Map.centerandzoom (Point, -);
Map.addoverlay (NewBmap.marker (point));
}}, "Beijing");
When the system cannot match "10 Street, Haidian District, Beijing", it will return to the geometric center point of "Haidian District, Beijing".
If it still doesn't match, it returns the geometric center point of "Beijing".

If you just want to return to the "Beijing" coordinates, or if you want to blur the query, it is recommended that you do not use address resolution.
Instead, use the search method of the Localsearch class. For examples, see:

When address resolution fails, you can call the Localsearch function. This method is called fuzzy query .

In the following example, for convenience, I took the first query result.

This way, when the address resolution "Nanning" failure, will automatically search this address, and on the map.

Full Source code:

<!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd ">
<title>Search when address resolution fails</title>
<Metaname= "Generator"content= "EditPlus">
<Metaname= "Author"content="">
<Metaname= "Keywords"content="">
<Metaname= "Description"content="">
<Scripttype= "Text/javascript"src= "http://api.map.baidu.com/api?v=1.2"></Script>

<DivID= "Divmap"style= "Width:400px;height:400px;border:solid 1px Gray"></Div>
<Scripttype= "Text/javascript">
varMap= NewBmap.map ("Divmap");
Map.centerandzoom (NewBmap.point (108.532769,22.825487), A); //Here's the definition of Nanning.
varGC= NewBmap.geocoder ();
Gc.getpoint ("Nanning Green Show District", function(PT) {
if(PT) {
Map.addoverlay (NewBmap.marker (PT)); //If the address resolution is successful, add the red marker
varls= NewBmap.localsearch ("Nanning");
Ls.search ("Nanning Green Show District");
Ls.setsearchcompletecallback (function(RS) {
if(Ls.getstatus ()==bmap_status_success) {
varPOI=Rs.getpoi (0); //fetch the 1th result of the query
if(POI) {
Map.addoverlay (NewBmap.marker (pt2)); //if queried, add red marker
Alert ("fail");
}, "Nanning");

"Baidu Map API" When address resolution fails, how to call the search method to find an address

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.