Get address and city name through latitude and longitude

Source: Internet
Author: User
Tags readline

The specific invocation method is as follows:

The code is as follows Copy Code

Jaddrobj = Getjsonaddr (string.valueof (Dlat),

String.valueof (Dlong));

if (jaddrobj==null) {

Jaddrobj = Sysutil.getjsonaddr (string.valueof (Dlat),

String.valueof (Dlong));

}

if (jaddrobj!=null) {

Jsobj = Jaddrobj.getjsonobject ("Status");

if (jsobj.getstring ("code"). Equals ("200")) {

Jplacearr = Jaddrobj.getjsonarray ("Placemark");

Jplaceobj = jplacearr.getjsonobject (0);

Address = jplaceobj.getstring ("Address");

grobaldata.myaddress = address;//The current address into a global variable

grobaldata.curaddress = address;//The current address into a global variable

City = Jplaceobj.getjsonobject ("Addressdetails"). Getjsonobject ("Country")

. Getjsonobject ("Administrativearea"). Getjsonobject ("locality")

. getString ("Localityname");

}

else {

Rtvalue = "Error";

}

}

/**

* The reverse resolution of the address according to latitude and longitude, sometimes need to try more than a few times

* Note: (Excerpt from: http://code.google.com/intl/zh-CN/apis/maps/faq.html

* Is there a limit on the number of address resolution requests submitted? If you receive more than 2,500 address resolution requests from an IP address within a 24-hour period, or from an IP

* Address resolution request rate is too fast, the Google map API Encoder will begin to respond with 620 status code. If the address resolver is still used too much, from the

The code is as follows Copy Code

* Access to the Google maps API address parser by IP addresses may be permanently blocked.

*

* @param latitude

* Latitude

* @param Longitude

* Longitude

* @return Jsonobject

*

* @author Lvqiyong

*/

/* Examples are as follows:

* {

"Name": "29.871398,121.5817",

' Status ': {

"Code": 200,

"Request": "GeoCode"

},

"Placemark": [{

"id": "P1",

"Address": "No. No. 360 Zhongxing Road, Jiangdong District, Ningbo, Zhejiang, China: 315040",

"Addressdetails": {

"Accuracy": 8,

"Country": {

"Administrativearea": {

"Administrativeareaname": "Zhejiang Province",

"Locality": {

"Dependentlocality": {

"Dependentlocalityname": "Jiangdong District",

"Thoroughfare": {

"Thoroughfarename": "No. No. 360 Zhongxing Road"

}

},

"Localityname": "Ningbo"

}

},

"CountryName": "China",

"Countrynamecode": "CN"

}

},

"Extendeddata": {

"Latlonbox": {

"North": 29.8737610,

"South": 29.8710630,

"East": 121.5832290,

"West": 121.5805310

}

},

"Point": {

"Coordinates": [121.5818800, 29.8724120, 0]

}

} ]

}*/

public static Jsonobject getjsonaddr (string latitude, string longitude) {

It can be http://maps.google.cn/maps/geo?output=csv&key=abcdef&q=%s,%s, but it's an English address.

The key can be casually written a key=abc

Output=csv, or it can be XML or JSON, the default way is to Output=json

String url = string

. Format (

"Http://ditu.google.cn/maps/geo?key=abcdef&q=%s,%s",

latitude, longitude);

HttpGet httpget = new HttpGet (URL);

HttpClient client = new Defaulthttpclient ();

HttpResponse response;

StringBuilder StringBuilder = new StringBuilder ();

try {

Response = Client.execute (HttpGet);

/* httpentity entity = response.getentity ();

InputStream stream = Entity.getcontent ();

int b;

while ((b = stream.read ())!=-1) {

Stringbuilder.append ((char) b);

}*/

httpentity entity = response.getentity ();

BufferedReader br = new BufferedReader (New InputStreamReader (Entity

. getcontent ());

String result = Br.readline ();

while (result!= null) {

Stringbuilder.append (result);

result = Br.readline ();

}

catch (Clientprotocolexception e) {

catch (IOException e) {

}

Jsonobject jsonobject = new Jsonobject ();

try {

Jsonobject = new Jsonobject (stringbuilder.tostring ());

catch (Jsonexception e) {

E.printstacktrace ();

}

return jsonobject;

}

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.