package com.pb.baiduapi; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; /** * 獲取經緯度 * * @author jueyue 返回格式:Map<String,Object> map map.put("status", * reader.nextString());//狀態 map.put("result", list);//查詢結果 * list<map<String,String>> * 密鑰:f247cdb592eb43ebac6ccd27f796e2d2 */ public class GetLatAndLngByBaidu { /** * @param addr * 查詢的地址 * @return * @throws IOException */ public Object[] getCoordinate(String addr) throws IOException { String lng = null;//經度 String lat = null;//緯度 String address = null; try { address = java.net.URLEncoder.encode(addr, "UTF-8"); }catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } String key = "f247cdb592eb43ebac6ccd27f796e2d2"; String url = String .format("http://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();// 不使用代理 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(","));//經度 count++; }else if(count==6){ lat = data.substring(data.indexOf(":")+1);//緯度 count++; }else{ count++; } } } } catch (IOException e) { e.printStackTrace(); } finally { if(insr!=null){ insr.close(); } if(br!=null){ br.close(); } } return new Object[]{lng,lat}; } public static void main(String[] args) throws IOException { GetLatAndLngByBaidu getLatAndLngByBaidu = new GetLatAndLngByBaidu(); Object[] o = getLatAndLngByBaidu.getCoordinate("成都市天府四街"); System.out.println(o[0]);//經度 System.out.println(o[1]);//緯度 } }
輸出結果:104.04701
30.548397
調用以下路徑根據經緯度獲取地址,返回數據為JSON格式,
http://api.map.baidu.com/geocoder/v2/?ak=pmCgmADsAsD9rEXkqWNcTzjd&location=30.548397,104.04701&output=json&pois=1
返回結果:
{"status":0,"result":{"location":{"lng":104.04701001273,"lat":30.548396923974},"formatted_address":"四川省成都市武侯區天府四街","business":"","addressComponent":{"city":"成都市","district":"武侯區","province":"四川省","street":"天府四街","street_number":""},"pois":[{"addr":"四川省成都市武侯區天府四街","cp":"NavInfo","distance":"144","name":"保利·心語花園","poiType":"地產小區","point":{"x":104.04823889466,"y":30.54874673449},"tel":"","uid":"881965b85a8d8a977369d348","zip":""},{"addr":"四川省成都市武侯區天府四街","cp":"NavInfo","distance":"629","name":"逸都國際","poiType":"地產小區","point":{"x":104.05247889667,"y":30.547152788724},"tel":"","uid":"e49f869c4f156be9c6effb6b","zip":""},{"addr":"四川省成都市武侯區劍南大道","cp":"NavInfo","distance":"881","name":"世豪廣場","poiType":"地產小區","point":{"x":104.05252381195,"y":30.553318490823},"tel":"","uid":"a2a7b0b167a0e1997adbd206","zip":""},{"addr":"四川省成都市武侯區德賽三街","cp":"NavInfo","distance":"989","name":"顧客停車場","poiType":"交通設施","point":{"x":104.04942465794,"y":30.555798655385},"tel":"","uid":"d135a6e15571911483a00cfe","zip":""}],"cityCode":75}}
JS方法
// 首先引用百度 script <script type="text/javascript" src="http://api.map.baidu.com/api?type=quick&ak=百度密鑰&v=1.0"></script> // 然后 javascript : // 百度地圖API功能,反之獲取百度坐標點 var map = new BMap.Map("allmap"); var point = new BMap.Point(116.331398,39.897445); var gc = new BMap.Geocoder(); gc.getLocation(point, function(rs){ var addComp = rs.addressComponents; alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber); });
地圖狀態方法
方法 | 返回值 | 描述 |
---|---|---|
getBounds() | Bounds | 返回地圖可視區域,以地理坐標表示。 |
getCenter() | Point | 返回地圖當前中心點。 |
getDistance(start:Point, end:Point) | Number | 返回兩點之間的距離,單位是米。(自 1.1 新增) |
getMapType() | MapTypes | 返回地圖類型。(自 1.1 新增) |
getSize() | Size | 返回地圖視圖的大小,以像素表示。 |
getViewport(view: Array<Point>[, viewportOptions: ViewportOptions]) | Viewport | 根據提供的地理區域或坐標獲得最佳的地圖視野,返回的對象中包含center和zoom屬性,分別表示地圖的中心點和級別。此方法僅返回視野信息,不會將新的中心點和級別做用到當前地圖上。(自 1.1 新增) |
getZoom() | Number | 返回地圖當前縮放級別。 |
getPanorama()new | Panorama | 獲取與Map類綁定的全景實例。(自 2.0新增) |
構造函數
構造函數 | 描述 |
---|---|
Geocoder() | 創建一個地址解析器的實例 |
方法
方法 | 返回值 | 描述 |
---|---|---|
getPoint(address:String, callback:Function, city:String) | none | 對指定的地址進行解析。如果地址定位成功,則以地址所在的坐標點Point為參數調用回調函數。否則,回調函數的參數為null。city為地址所在的城市名,例如“北京市”。 |
getLocation(point:Point, callback:function[, options: LocationOptions]) | none | 對指定的坐標點進行反地址解析。如果解析成功,則回調函數的參數為GeocoderResult對象,否則回調函數的參數為null。(自 1.1 新增) |