Java Baidu map latitude and longitude correction, java latitude and longitude correction

Source: Internet
Author: User

Java Baidu map latitude and longitude correction, java latitude and longitude correction

In China, the longitude and latitude obtained by using electronic maps are not real longitude and latitude, but an offset is added to the real latitude and longitude through some algorithms, and different maps have different algorithms. Now I want to tell you how to correct Baidu map in java, mainly to display the real longitude and latitude on Baidu map to eliminate the deviation.

1. If you need to offset a small amount of latitude and longitude, you can do it directly in the browser. Baidu provides the corresponding API

1, API address: http://api.map.baidu.com/ag/coord/convert? From = 0 & to = 4 & x = 113.240324 & y = 23.817349

From = 0: indicates that the input is a real latitude and longitude.

To = 4: After Baidu correction is returned, the longitude and latitude of the address can be correctly displayed on Baidu map.

X: longitude y: latitude

Returned data: {"error": 0, "x": "MTEzLjI1MjIyMjUxOTg1", "y": "MjMuODIwNjM5MTEyNDgy "}

The returned data is encrypted by Base64. You can find a website that is online Base64 decrypted.

 

2. If the data volume is large, it is inconvenient to use the above method. Here we provide the Java method for batch offset. The Code is as follows:

1 import java. io. IOException; 2 import org. apache. http. httpEntity; 3 import org. apache. http. httpResponse; 4 import org. apache. http. client. clientProtocolException; 5 import org. apache. http. client. responseHandler; 6 import org. apache. http. client. methods. httpPost; 7 import org. apache. http. impl. client. closeableHttpClient; 8 import org. apache. http. impl. client. httpClients; 9 import org. apache. http. util. E NtityUtils; 10 import net. sf. json. JSONObject; 11 12 public class Remove {13 public static void main (String [] args) {14 try {15 // there is only one data record. If there are multiple data entries, you can use a loop, then splice the url String 16 String url = "http://api.map.baidu.com/ag/coord/convert? From = 0 & to = 4 & x = 113.540124 & y = 23.517846 "; 17 JSONObject json = getAllEmployee (url ); 18 // decode the longitude and latitude and print 19 String latitude = decode (json. getString ("x"); 20 String longpolling = decode (json. getString ("y"); 21 System. out. println ("longitude:" + latitude); 22 System. out. println ("latitude:" + longpolling); 23} catch (Exception e) {24 e. printStackTrace (); 25} 26} 27 28/** 29 * Java background access url link, return data in JSON format 30 * @ return31 */32 public Static JSONObject getAllEmployee (String url) {33 try {34 CloseableHttpClient httpclient = HttpClients. createDefault (); 35 HttpPost httpPost = new HttpPost (url); 36 ResponseHandler <JSONObject> responseHandler = new ResponseHandler <JSONObject> () {37 // after the connection is successfully called, operation on returned data 38 public JSONObject handleResponse (final HttpResponse response) 39 throws ClientProtocolException, IOException {40 int status = respo AUC. getStatusLine (). getStatusCode (); 41 if (status> = 200 & status <300) {42 // get the data returned after successful call 43 HttpEntity entity = response. getEntity (); 44 if (null! = Entity) {45 String result = EntityUtils. toString (entity); 46 // generate a JSON object 47 JSONObject resultObj = JSONObject Based on the string. fromObject (result); 48 return resultObj; 49} else {50 return null; 51} 52} else {53 throw new ClientProtocolException ("Unexpected response status:" + status ); 54} 55} 56}; 57 // returned json object 58 JSONObject responseBody = httpclient.exe cute (httpPost, responseHandler); 59 return responseBody; 60} catch (Exception e) {61 e. printStackTrace (); 62 return null; 63} 64} 65 66/** 67 * Base64 decoding 68 * @ param str69 * @ return70 */71 public static String decode (String str) {72 byte [] bt = null; 73 String s = ""; 74 try {75 sun. misc. BASE64Decoder decoder = new sun. misc. BASE64Decoder (); 76 bt = decoder. decodeBuffer (str); 77 s = new String (bt, "GB2312"); 78} catch (IOException e) {79 e. printStackTrace (); 80} 81 return s; 82} 83}

3. The jar package required to run the above Code is as follows:

1, http://pan.baidu.com/s/1qX7Zipe password: 0rqq

Related Article

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.