当前位置:   article > 正文

java调用百度地图API根据地理位置获取经纬度_java 百度地图xy轴坐标转经纬度

java 百度地图xy轴坐标转经纬度

前置条件

由于我们是使用百度提供的地理API接口,所以事先我们需要创建百度账号,并创建百度应用(因为我们要用应用到的ak)~~~~
偷偷的告诉你们…这是免费的哦不要钱的…

我们先来创建百度应用

百度链接: http://lbsyun.baidu.com/apiconsole/key在这里插入图片描述

创建完毕之后我们就可以正式的开发了

*通过本章节你将了解到*

1,通过地理位置,获取到详细的经纬度

先导入一下我们的httpclient

  	   <dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.5.3</version>
		</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
	/**
	 * http://lbsyun.baidu.com/apiconsole/key
	 * <百度开发者>用户申请注册的key,自v2开始参数修改为“ak”,之前版本参数为“key” 申请ak
	 */
	final static String AK = "edGc5mIugVxx7lwUx9YpraKeWmExG64o";


	/**
	 * 地理编码 URL
	 */
	final static String ADDRESS_TO_LONGITUDEA_URL = "http://api.map.baidu.com/geocoding/v3/?output=json&location=showLocation";

	/**
	 * 地理编码
	 * @param address  (广东省广州市黄埔区)
	 *         详细的位置信息
	 * @return
	 */
	public ReturnLocationBean AddressTolongitudea(String address) {

         if(StringUtils.isBlank(address)){
        	
        	 return null;
         }
        
		String url = ADDRESS_TO_LONGITUDEA_URL + "&ak=" + AK + "&address="+ address;
		log.info("请求url:" + url);
		HttpClient client = HttpClients.createDefault(); // 创建默认http连接
		HttpPost post = new HttpPost(url);// 创建一个post请求

		try {
			HttpResponse response = client.execute(post);// 用http连接去执行get请求并且获得http响应
			HttpEntity entity = response.getEntity();// 从response中取到响实体
			String html = EntityUtils.toString(entity);// 把响应实体转成文本
			log.info("返回信息:" + html);
			// JSON转对象
			return JSON.parseObject(html, ReturnLocationBean.class);
		} catch (Exception e) {

			log.error("地理编码[异常],", e);
			return null;
		}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

返回信息
在这里插入图片描述

2,通过经纬度,获取到详细地理位置信息**

	/**
	 * 逆地理编码 URL
	 */
	final static String LONGITUDE_TO_ADDRESS_URL = "http://api.map.baidu.com/reverse_geocoding/v3/?output=json&coordtype=BD09&pois=1";
   
     	
	/**
	 * 逆地理编码
	 * @param lat 
	 *        纬度 23.1067,
	 * @param lng 
	 *        经度 113.325
	 * @return
	 */ 
    public ReturnLocationBean longitudeToAddress(float lat, float lng) {

		String url = LONGITUDE_TO_ADDRESS_URL + "&ak=" + AK + "&location=" + lat + "," + lng;
		log.info("请求url:" + url);
		HttpClient client = HttpClients.createDefault(); // 创建默认http连接
		HttpPost post = new HttpPost(url);// 创建一个post请求

		try {

			HttpResponse response = client.execute(post);// 用http连接去执行get请求并且获得http响应
			HttpEntity entity = response.getEntity();// 从response中取到响实体
			String html = EntityUtils.toString(entity);// 把响应实体转成文本
			log.info("返回信息:" + html);
			// JSON转对象
			return JSON.parseObject(html, ReturnLocationBean.class);
		} catch (Exception e) {

			log.error("逆地理编码[异常],", e);
			return null;
		}

	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

返回信息如下
在这里插入图片描述

3,不通源坐标之间的相互转换

 之所以会写这个是因为我公众号的经纬度是腾讯提供的。。。。所以要转一次。。。。。
  • 1
 
  
     void fun() throws IOException{
	   	
	   	   String url = "http://api.map.baidu.com/geoconv/v1/?coords=113.538248,23.132953&from=1&to=5&ak=edGc5mIugVxx7lwUx9YpraKeWmExG64o";
	   	
	       HttpClient client = HttpClients.createDefault();// 创建默认http连接
	       HttpPost post = new HttpPost(url);// 创建一个post请求
	      
	       HttpResponse response = client.execute(post);// 用http连接去执行get请求并且获得http响应
	       HttpEntity entity = response.getEntity();// 从response中取到响实体
	       String html = EntityUtils.toString(entity);// 把响应实体转成文本
	       System.out.println("返回信息"+html);
	  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

from参数说明
源坐标类型:
1:GPS设备获取的角度坐标,WGS84坐标;
2:GPS获取的米制坐标、sogou地图所用坐标;
3:google地图、soso地图、aliyun地图、mapabc地图和amap地图所用坐标,国测局(GCJ02)坐标;
4:3中列表地图坐标对应的米制坐标;
5:百度地图采用的经纬度坐标;
6:百度地图采用的米制坐标;
7:mapbar地图坐标;
8:51地图坐标
返回信息

{“status”:0,“result”:[{“x”:113.54988989895114,“y”:23.13628309504525}]}


x float 经度
y float 纬度


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/181478
推荐阅读
相关标签
  

闽ICP备14008679号