做项目时,遇到对地点获取地图中对应的经纬度,作一下笔记,以备以后直接使用

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 = null;
while ((data = br.readLine()) != null) {
buf.append(data.replace(":", ""));
// 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 = null;
while ((data = br.readLine()) != null) {
System.out.println(data);
}
insr.close();
}
} catch (IOException e) {
e.printStackTrace();
} } public static void main(String[] args) throws IOException {
String str = "新疆维吾尔自治区哈密市南湖乡大南湖矿区";
GetLatAndLngByBaidu lotAndLng = new GetLatAndLngByBaidu();
String result = GetLatAndLngByBaidu.getCoordinate(str).replace(":", "");
System.out.println(result);
String[] loc = result.split(",");
lotAndLng.getCoordinate(loc[0], loc[1]);
}
}

根据百度地图API获取指定地点的经纬度的更多相关文章

  1. python实现百度地图API获取某地址的经纬度

    申请百度API 1.打开网页 http://lbsyun.baidu.com/index.php?title=首页 选择功能与服务中的地图,点击左边的获取密匙,然后按照要求申请即可,需要手机和百度账号 ...

  2. HTML5调用百度地图API获取当前位置并直接导航目的地的方法

    <!DOCTYPE html> <html lang="zh-cmn-Hans">     <meta charset="UTF-8&quo ...

  3. HTML5页面直接调用百度地图API,获取当前位置,直接导航目的地

    <!DOCTYPE html> <html lang="zh-cmn-Hans"> <meta charset="UTF-8"&g ...

  4. Python 读取照片的信息:拍摄时间、拍摄设备、经纬度等,以及根据经纬度通过百度地图API获取位置

    通过第三方库exifread读取照片信息.exifread官网:https://pypi.org/project/ExifRead/ 一.安装exifreadpip install exifread ...

  5. PhoneGap Geolocation结合百度地图api获取地理位置api

    一.使用百度地图API 1.地址:http://developer.baidu.com/map/ 2.在js DEMO中获取反地址解析的DEMO 3.修改这个DEMO的密钥,去创建应用就能创建密钥,然 ...

  6. 百度地图API获取数据

    目前,大厂的服务范围越来越广,提供的数据信息也是比较全的,在生活服务,办公领域,人工智能等方面都全面覆盖,相对来说,他们的用户基数大,通过用户获取的信息也是巨大的.除了百度提供api,国内提供免费AP ...

  7. 批量调用百度地图API获取地址经纬度坐标

    1 申请密匙 注册百度地图API:http://lbsyun.baidu.com/index.php?title=webapi 点击左侧 “获取密匙” ,经过填写个人信息.邮箱注册等,成功之后在开放平 ...

  8. vue项目使用百度地图API获取经纬度

    一.首先在百度api注册获得ak密钥 二.进行引入 (1).第一种方式: 直接在vue中index.html中用script标签引入. //你的ak密钥需要替换真实的你的ak码 <script ...

  9. 通过netty把百度地图API获取的地理位置从Android端发送到Java服务器端

    本篇记录我在实现时的思考过程,写给之后可能遇到困难的我自己也给到需要帮助的人. 写的比较浅显,见谅. 在写项目代码的时候,需要把Android端的位置信息传输到服务器端,通过Netty达到连续传输的效 ...

随机推荐

  1. BZOJ3098 Hash Killer II 【概率】

    挺有意思的一题 就是卡一个\(hash\) 我们先取L大概几十保证结果会超出\(10^9 + 7\) 然后就随机输出\(10^5\)个字符 由题目的提示我们可以想到,如果我们有\(n\)个数,选\(k ...

  2. TroubleShoot: SharePoint 2013: ExecuteOrDelayUntilScriptLoaded 页面发布后不执行的问题

    SharePoint 2010 中的ExecuteOrDelayUntilScriptLoaded,在2013 中使用时没有效果的问题. Example: SharePoint 2013 Code: ...

  3. POJ 3037 Skiing

    Skiing Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4810   Accepted: 1287   Special ...

  4. 【Visual Studio】Windows program compatibility mode is on, turn it off……

    [问题描述]Windows 10上安装 Visual Studio Ultimate 2013,出现下面错误: [解决方案]将.iso文件解压到硬盘再安装.

  5. c#使用椭圆签名算法制作软件序列号

    椭圆曲线密码学(Elliptic curve cryptography,缩写为ECC)是基于椭圆曲线数学的一种公钥密码的方法.椭圆曲线在密码学中的使用是在1985年由Neal Koblitz和Vict ...

  6. java通过代码控制线程状态,解决线程不安全的问题。

    写两个类,Input,output 两个都是使用同步代码块的方式实现线程间的同步 input类,是为变量赋值 output类,是打印变量 由于线程争夺cpu造成数据的不匹配 通过,设立一个 flag ...

  7. hdu 5459(递推好题)

    Jesus Is Here Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)To ...

  8. LeetCode OJ-- Trapping Rain Water*

    https://oj.leetcode.com/problems/trapping-rain-water/ 模拟题,计算出在凹凸处存水量. 对于一个位置 i ,分别计算出它左边的最大值 left (从 ...

  9. LeetCode OJ--Rotate Image

    http://oj.leetcode.com/problems/rotate-image/ 将矩阵顺时针旋转90度.要求为原地算法. 注意对输入的测试,矩阵为空,长度为1,为2时…… #include ...

  10. spring mvc 编写处理带参数的Controller

    在上一随笔记录的基础上,现记录编写处理带有参数的Controller. @Controller //这个注解会告知<context:component:scan> 将HomeControl ...