Use the Baidu Map geocoding API to determine the longitude latitude of road name, landmark building and market name

Source: Internet
Author: User
Tags log

It is now often necessary to provide some information about location based on the location provided by the user. Sometimes you can directly determine the latitude and longitude of the user, sometimes not necessarily determine the user's latitude and longitude information, the user is through the input of some road name, landmark building or store name location, but our database may not be stored in the user may enter the latitude of these location information, At this point you can use some map-provided APIs to determine the longitude and latitude of the location information that the user enters.

We use the Geocoding API provided by the Baidu map to achieve the conversion from location information to longitude latitude, detailed instructions can refer to the Geocoding API. Let's do a simple demo here.

public string Getgeocode (string query) throws Clientprotocolexception, ioexception{
    httpclient httpclient = new Defaulthttpclient ();
    String url = geocoderequesturl (query);
    Logger.log (level.info, url);
    HttpGet httpget = new HttpGet (URL);
    responsehandler<string> ResponseHandler = new Basicresponsehandler ();
    String responsebody = Httpclient.execute (HttpGet, ResponseHandler);//Baidu returns longitude latitude information xml
    logger.log (Level.info, " Baidu response: "+responsebody);
    return responsebody;
}
    
public string Geocoderequesturl (string query) throws unsupportedencodingexception{
    string url = Wechatconstant.baseurl + "geocoder?address=" + urlencoder.encode (query, "UTF-8") + "&key=" +
            Wechatconstant.mapkey + "&output=" + Wechatconstant.outputformat;
    return URL;

Using JUnit for testing

@Test public
void Testgeocode () throws Exception {
    Baidumapservice BMS = new Baidumapservice ();
    String response = Bms.getgeocode ("10 Street, No. tenth");
    Baidugeocoderesponse res = Baidugeocoderesponse.getbaidugeocode (response);//Parse XML
    System.out.println ( Res.tostring ());
}

Results of output

<GeocoderSearchResponse> 
    <status>OK</status>
    <result>
                    <location>
                <lat>40.057098</lat>
                <lng>116.307175</lng>
            </location>    
            < precise>1</precise>
            <confidence>80</confidence>
            <level> Road </level>
            </result>    
</GeocoderSearchResponse>
baidugeocoderesponse [lat=40.057098, lng= 116.307175]

Source: http://www.qiyadeng.com/

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.