1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 使用百度地图API获取经纬度

使用百度地图API获取经纬度

时间:2023-10-29 01:21:58

相关推荐

使用百度地图API获取经纬度

注意事项:

1. 使用 百度地图API 需要先申请 ak(API 密钥)。

事例 java 代码如下:

package test;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import .MalformedURLException;

import .URL;

import .URLConnection;

import net.sf.json.JSONObject;

public class TestBaiduMapAPI {

/*** description: This method was used to get longitude and latitude. * @param address*/public static void test(String address){// address: 地址output:输出格式ak:是申请的百度 api 密钥String path = "http://api./geocoder/v2/?address="+address+"&output=json&ak=####################";String lineData = null;StringBuilder sb = new StringBuilder();try {URL url = new URL(path);URLConnection conn = url.openConnection();BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));while((lineData = br.readLine()) != null){sb.append(lineData);}JSONObject json = JSONObject.fromObject(sb.toString());JSONObject result = json.getJSONObject("result").getJSONObject("location");String lng = result.getString("lng");String lat = result.getString("lat");System.out.println("coordinate:("+lng+", "+ lat+");");} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void main(String[] args) {test("中国江苏省连云港市");}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。