Java backend method for obtaining corresponding coordinate values by using parameters

Source: Internet
Author: User

Public list<string> Getmap (string address, string city) {
String returnxml= "";
List<string> list=new arraylist<string> ();
try {
Address=urlencoder.encode (Address, "UTF-8");
City=urlencoder.encode (city, "UTF-8");
String urls= "http://api.map.baidu.com/geocoder/v2/?ak=07dbeabb4d734364eea7f2eb49eb472b&callback= renderoption&output=xml&address= "+address+" &city= "+city;
url url =new url (URLs);
HttpURLConnection conn = (httpurlconnection) url.openconnection ();
Conn.setconnecttimeout (5*1000);
Conn.setrequestmethod ("GET");

Parse the returned XML message body
byte[] msgbody = null;
if (Conn.getresponsecode ()! = 200) {
SYSTEM.OUT.PRINTLN ("error");
}
InputStream is = (InputStream) conn.getinputstream ();//Get Return Data

byte[] temp = new byte[1024];
int n = 0;
Bytearrayoutputstream BOS = new Bytearrayoutputstream ();
while ((n = is.read (temp))! =-1)
{
Bos.write (temp, 0, N);
}
Msgbody = Bos.tobytearray ();
Bos.close ();
Is.close ();
Returnxml= New String (Msgbody, "UTF-8"). Trim ();
Conn.disconnect ();

The following parsing XML
System.out.println (Returnxml);
Map<string, string> map=new hashmap<string, string> ();
Saxreader reader=new Saxreader ();
Document document = Reader.read (New Bytearrayinputstream (Returnxml.getbytes ("UTF-8"));
Element rootelement = Document.getrootelement ();
for (Iterator Iterator = Rootelement.elementiterator (); Iterator.hasnext ();) {
Element root = (element) Iterator.next ();
For (Iterator Iterator1 =root.elementiterator (); Iterator1.hasnext ();) {
Element it = (element) Iterator1.next ();
for (Iterator Iterator2 = It.elementiterator (); Iterator2.hasnext ();) {
Element it1 = (Element) Iterator2.next ();
if (it1.getname () = = "Lat") {
String name = It1.getname ();
String text = It1.gettexttrim ();
Map.put (name, text);
}if (it1.getname () = = "LNG") {
String name = It1.getname ();
String text = It1.gettexttrim ();
Map.put (name, text);
}
}
}
}
For (String Key:map.keySet ()) {
List.add (Map.get (key));
}
} catch (Exception e) {
E.printstacktrace ();
}
return list;
}

I passed the parameter is the province (city) and the specific address, returned to get an XML file. By parsing the XML to get the corresponding coordinate value, the return can also be a JSON-formatted file, like the example given below:

private void Convertgps2baidu (list<dbobject> gpsdata) {

String ak = configproperties.getproperty ("Map_ak");
String ak = "07dbeabb4d734364eea7f2eb49eb472b";
String from = "1";
String to = "5";

String url = configproperties.getproperty ("Map_url");
String url = "http://api.map.baidu.com/geoconv/v1/";
Resty r = new Resty ();

int maxCount = 100;

int loopcount = Gpsdata.size ()/maxCount;
if (gpsdata.size ()% MaxCount > 0) {
loopcount++;
}

for (int i = 0; i < Loopcount; i++) {
String GPS = "";

for (int j = 0; J < MaxCount; J + +) {
int k = j + (I * maxCount);
if (k >= gpsdata.size ()) {
Break
}

DBObject dbobj = Gpsdata.get (k);
DBObject gpsobj = (dbobject) dbobj.get ("GPs");
GPS + = Gpsobj.get ("latitude"). ToString () + "," + Gpsobj.get ("longitude"). ToString () + ";";
}

Remove the last semicolon
if (gps.length () > 0) {
GPS = gps.substring (0, Gps.length ()-1);
}

Jsonresource obj;
try {
obj = R.json (URL,
Form (Data ("Coords", GPS), data ("AK", AK), data ("from", "from", "Data" ("to"));
int status = (Integer) obj.get ("status");
if (status = = 0) {
Jsonarray Baidugps = (jsonarray) obj.get ("result");
for (int j = 0; J < Baidugps.length (); j + +) {
DBObject baiduobj = Gpsdata.get (j + (I * maxCount));
Jsonobject Baidumap = (jsonobject) baidugps.get (j);
DBObject baidumongoobj = new Basicdbobject ();
Baidumongoobj.put ("X", Baidumap.get ("X"));
Baidumongoobj.put ("Y", Baidumap.get ("Y"));
Baidumongoobj.put ("S", ((dbobject) baiduobj.get ("GPs")). Get ("VSS"));

Baiduobj.put ("B_gps", baidumongoobj);
Vehdatadao.savegpsdata (Baiduobj);
}
} else {
Logger.error ("Baidu GPS convert error .... status=" + status);
Logger.error ("GPs row string:" + GPS);
}

} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

}

Java backend method for obtaining corresponding coordinate values by using parameters

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.