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/