Google Map API怎么根据地址获得经纬度
1个回答
2016-04-25
展开全部
/**
* 根据地址返回经纬度
* @param addr
* @return 返回经纬度数据, latLng[0]经度,latLng[1]维度
*/
public static String[] getCoordinate(String addr) {
String[] latLng = new String[2];
String address = null;
try {
address = java.net.URLEncoder.encode(addr, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
;
String output = "csv";
//密钥可以随便写一个key=abc
String key = "abc";
String url = "http://maps.google.com/maps/geo?q=" + address + "&output=" + output + "&key=" + key;
URL googleMapURL = null;
URLConnection httpsConn = null;
// 进行转码
try {
googleMapURL = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
httpsConn = (URLConnection)googleMapURL.openConnection();
if (httpsConn != null) {
InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream(), "UTF-8");
BufferedReader br = new BufferedReader(insr);
String data = null;
if ((data = br.readLine()) != null) {
String[] retList = data.split(",");
/*
* String latitude = retList[2]; String longitude =
* retList[3];
*
* System.out.println("纬度"+ latitude);
* System.out.println("经度"+ longitude);
*/
if (retList.length > 2 && ("200".equals(retList[0]))) {
latLng[0] = retList[2];
latLng[1] = retList[3];
}
}
insr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return latLng;
}
* 根据地址返回经纬度
* @param addr
* @return 返回经纬度数据, latLng[0]经度,latLng[1]维度
*/
public static String[] getCoordinate(String addr) {
String[] latLng = new String[2];
String address = null;
try {
address = java.net.URLEncoder.encode(addr, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
;
String output = "csv";
//密钥可以随便写一个key=abc
String key = "abc";
String url = "http://maps.google.com/maps/geo?q=" + address + "&output=" + output + "&key=" + key;
URL googleMapURL = null;
URLConnection httpsConn = null;
// 进行转码
try {
googleMapURL = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
httpsConn = (URLConnection)googleMapURL.openConnection();
if (httpsConn != null) {
InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream(), "UTF-8");
BufferedReader br = new BufferedReader(insr);
String data = null;
if ((data = br.readLine()) != null) {
String[] retList = data.split(",");
/*
* String latitude = retList[2]; String longitude =
* retList[3];
*
* System.out.println("纬度"+ latitude);
* System.out.println("经度"+ longitude);
*/
if (retList.length > 2 && ("200".equals(retList[0]))) {
latLng[0] = retList[2];
latLng[1] = retList[3];
}
}
insr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return latLng;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询