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

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. BZOJ3196 二逼平衡树 【线段树套平衡树】

    题目 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排名 2.查询区间内排名为k的值 3.修改某一位值上的数值 4.查询k在区间内的前驱(前驱 ...

  2. Github管理 第二步:Eclipse+Github,管理Java Project版本(First Commit)

    1.提醒 如果你的Eclipse和本文一样操作,却出现了不同的结果和莫名其妙的错误,换个Eclipse也许更快. 我用了2个Eclipse,第一个一步一个坑,第2个非常顺利…… 所以,继Windows ...

  3. py-faster-rcnn 的makefile.config 注意事项

    在配置py-faster-rcnn的过程中,我遇到一些问题,记录如下 py-faster-rcnn文件夹下面有一个caffe-fast-rcnn文件夹 这个过程中,我们需要编译caffe, rbgir ...

  4. html-屏蔽按键盘空格键是滚动条向下滚动

    document.onkeydown = function(ev){ var e = ev || event; if(e.keyCode == 32){ return false; } }

  5. python 中各种数据类型的排序问题

    list #按照list的第二键值排序 disP2P = [[1,2,3],[2,3,4],[4,5,6]] disP2P = sorted(disP2P,key = lambda x:x[2]) s ...

  6. Java 添加播放MIDI音乐

      Java 在多媒体处理方面的确优势不大,但是我们在程序中有些时候又需要一些音乐. 如果播放的音乐是wav等波形音频文件,又很大的话,所以背景音乐最好就是MIDI了. 网上很多播放MIDI的教程都是 ...

  7. git commit或pull后恢复到原来版本

    https://blog.csdn.net/litao31415/article/details/87713712

  8. ArrayList和LinkedList学习

    摘要 ArrayList和LinkedList是对List接口的不同数据结构的实现.它们都是线程不安全的,线程不安全往往出现在数组的扩容.数据添加的时候. 一.ArrayList和LinkedList ...

  9. Go -- socket read的内容以16进制输出

    buf := make([]) reqLen, err := conn.Read(buf) // 遍历, 转为16进制 buffer := new(bytes.Buffer) for _, b := ...

  10. 算法之美--2.3.1 Z字形编排问题

    2016-12-08   00:23:11 写在前面的话:万事贵在坚持,万事开头难,有很多的东西要学,要知道主次,讲究效率,大的方向对就行!坚持........ 一.图像压缩编码中的Z字排序 JPEG ...