Obtain the longitude and latitude of a certain place & amp; obtain the corresponding geographical location through the latitude and longitude, and obtain the latitude and longitude

Source: Internet
Author: User

Obtain the longitude and latitude of a certain place & obtain the corresponding geographical location through the latitude and longitude, get the latitude and longitude

Recently, we have to use a latitude and longitude to determine whether the latitude and longitude are located in a certain region. Therefore, we searched for information online and integrated the following content.

1. Obtain the longitude and latitude of the changed address through the address

/*** @ Param addr * query address * @ return * @ throws IOException */public Object [] getCoordinate (String addr) throws IOException {String lng = null; // longitude String lat = null; // latitude String address = null; try {address = java.net. URLEncoder. encode (addr, "UTF-8");} catch (UnsupportedEncodingException e1) {e1.printStackTrace ();} String key = "f247cdb592eb43ebac6ccd27f796e2d2"; String url = String. format ("htt P: // api.map.baidu.com/geocoder? Address = % s & output = json & key = % s ", address, key); URL myURL = null; URLConnection httpsConn = null; try {myURL = new URL (url);} catch (MalformedURLException e) {e. printStackTrace ();} InputStreamReader insr = null; BufferedReader br = null; try {httpsConn = (URLConnection) myURL. openConnection (); // No proxy if (httpsConn! = Null) {insr = new InputStreamReader (httpsConn. getInputStream (), "UTF-8"); br = new BufferedReader (insr); String data = null; int count = 1; while (data = br. readLine ())! = Null) {if (count = 5) {lng = (String) data. subSequence (data. indexOf (":") + 1, data. indexOf (","); // longitude count ++;} else if (count = 6) {lat = data. substring (data. indexOf (":") + 1); // latitude count ++;} else {count ++ ;}}} catch (IOException e) {e. printStackTrace ();} finally {if (insr! = Null) {insr. close ();} if (br! = Null) {br. close () ;}return new Object [] {lng, lat };}

 

Test

Public static void main (String [] args) throws IOException {GetLocation getLatAndLngByBaidu = new GetLocation (); Object [] o = getLatAndLngByBaidu. getCoordinate ("Tianfu Street, Chengdu"); System. out. println (o [0]); // longitude System. out. println (o [1]); // latitude}

 

Result:

104.06383230.54855

 

 

2. Obtain the address of the latitude and longitude through a certain latitude and longitude, Returns an object in json format (json.org needs to be introduced. the JSONObject class of the jar package is parsed. Here, only the city where the latitude and longitude are located can be queried through the latitude and longitude. Other information is available in json, which can be obtained through parsing, other information is not obtained here.

Public static void getCityFromLngAndlat () {// by modifying the location (latitude and longitude) parameter here, you can get the detailed information of the corresponding latitude and longitude String url2 = "http://api.map.baidu.com/geocoder/v2? Ak = pmCgmADsAsD9rEXkqWNcTzjd & location = 22.75424, 112.76535 & output = json & pois = 1 "; URL myURL2 = null; URLConnection httpsConn2 = null; try {myURL2 = new URL (url2 );} catch (MalformedURLException e) {e. printStackTrace ();} InputStreamReader insr2 = null; BufferedReader br2 = null; try {httpsConn2 = (URLConnection) myURL2.openConnection (); // if (httpsConn2! = Null) {insr2 = new InputStreamReader (httpsConn2.getInputStream (), "UTF-8"); br2 = new BufferedReader (insr2); String data2 = br2.readLine (); try {// The parsed json format data JSONObject dataJson = new JSONObject (data2); JSONObject result = dataJson. getJSONObject ("result"); JSONObject addressComponent = result. getJSONObject ("addressComponent"); String city = addressComponent. getString ("city"); System. out. pri Ntln ("city =" + city);} catch (JSONException e) {e. printStackTrace () ;}} catch (IOException e) {e. printStackTrace ();} finally {if (insr2! = Null) {insr2.close ();} if (br! = Null) {br2.close ();}}}

The raw json data obtained is as follows:

{"Status": 0, "result": {"location": {"lng": 112.76535000506, "lat": 22.754240005123}, "formatted_address ": "X492, Gaoming district, Foshan city, Guangdong Province", "business": "", "addressComponent": {"city": "Foshan city", "country": "China", "direction ": "", "distance": "", "district": "Gaoming district", "province": "Guangdong province", "street": "X492", "street_number ": "", "country_code": 0}, "pois": [{"addr": "Gaoming district, Foshan City", "cp": "NavInfo", "direction ": "Northeast China", "distance": "680", "name": "Soap screen mountain spring", "poiType": "company enterprise", "point": {"x ": 112.76102978437, "y": 22.750226120112}, "tag": "enterprise", "tel": "", "uid": "ea4a3dc9e82219534e5547fc", "zip ": "" },{ "addr": "near jingtau Kok, 4jiu second county", "cp": "NavInfo", "direction": "Northeast", "distance ": "690", "name": "country restaurant", "poiType": "food", "point": {"x": 112.76288029372, "y": 22.748959622733 }, "tag": "food; Chinese restaurant", "tel": "", "uid": "b292b3aa9bf25c28be48422f", "zip": "" },{ "addr ": "Near jingtou village, xizhou district", "cp": "NavInfo", "direction": "Northeast China", "distance": "724", "name ": "xiongying restaurant", "poiType": "food", "point": {"x": 112.76211673404, "y": 22.74900128402}, "tag": "food; chinese restaurant "," tel ":" "," uid ":" abe37f62d7e119b6cd04efd2 "," zip ":" "},{" addr ":" near jingtou village, xizhou 2 ", "cp": "NavInfo", "direction": "Northeast China", "distance": "730", "name": "Mingfeng restaurant", "poiType ": "food", "point": {"x": 112.75995181776, "y": 22.750776042908}, "tag": "food; Chinese restaurant", "tel ":"", "uid": "2fb890627b93c3a7c91b95e2", "zip": "" },{ "addr": "Yang he Zhen", "cp": "NavInfo", "direction ": "Northeast China", "distance": "757", "name": "mountain village Restaurant", "poiType": "food", "point": {"x": 112.76165859823, "y": 22.748934625955}, "tag": "food; Chinese restaurant", "tel": "", "uid": "637df60ac6f49c7241afe68b", "zip ": ""}], "poiRegions": [], "sematic_description": "680 metres northeast of zaomushan Spring", "cityCode": 138 }}

 

 

  

 

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.