首页 > 代码库 > 根据百度地图API获取指定地点的经纬度
根据百度地图API获取指定地点的经纬度
做项目时,遇到对地点获取地图中对应的经纬度,作一下笔记,以备以后直接使用
package com.hpzx.data;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;import com.hpzx.json.HTTPTokener;import com.hpzx.json.JSONException;/** * 获取经纬度通过 * * @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 static String getCoordinate(String addr) throws IOException { 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(); } httpsConn = (URLConnection) myURL.openConnection(); // 不使用代理 if (httpsConn != null) { InputStreamReader insr = null; insr = new InputStreamReader(httpsConn.getInputStream(), "UTF-8"); BufferedReader br = new BufferedReader(insr); StringBuffer buf = new StringBuffer(); String data = http://www.mamicode.com/null;":", "")); // System.out.println(data); } try { return getLoc(buf.toString()); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } public static String getLoc(String string) throws JSONException { StringBuffer buf = new StringBuffer(); HTTPTokener x = new HTTPTokener(string); String t; t = x.nextToken(); while (x.more()) { t = x.nextToken(); if (t.equalsIgnoreCase("lng")) { break; } } if (t.equalsIgnoreCase("lng")) { buf.append(x.nextToken()); if (x.nextToken().equals("lat")) { buf.append(x.nextToken()); } return buf.toString(); } return null; } public void getCoordinate(String longitude, String latitude) { String url = String .format("http://api.map.baidu.com/geocoder?output=json&" + "location=%s,%s%s&key=f247cdb592eb43ebac6ccd27f796e2d2", latitude, "%20", longitude); URL myURL = null; URLConnection httpsConn = null; try { myURL = new URL(url); } catch (MalformedURLException e) { e.printStackTrace(); } try { httpsConn = (URLConnection) myURL.openConnection(); if (httpsConn != null) { InputStreamReader insr = new InputStreamReader( httpsConn.getInputStream(), "UTF-8"); BufferedReader br = new BufferedReader(insr); String data = http://www.mamicode.com/null;"新疆维吾尔自治区哈密市南湖乡大南湖矿区"; GetLatAndLngByBaidu lotAndLng = new GetLatAndLngByBaidu(); String result = GetLatAndLngByBaidu.getCoordinate(str).replace(":", ""); System.out.println(result); String[] loc = result.split(","); lotAndLng.getCoordinate(loc[0], loc[1]); }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。