google map api如何根据地名获取经纬度???

aihuaying 2011-06-25 06:39:00
google map api如何根据地名获取经纬度???
function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map_canvas"));
//map.setMapType(G_SATELLITE_MAP);
map.setCenter(new GLatLng(26.263432028939793, 117.63870913535357), 13);

}
}

想把上面红色的经纬度,改为地名如:北京市。如何做???
如何根据地名获取经纬度???
...全文
645 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
aihuaying 2011-10-05
  • 打赏
  • 举报
回复
thank you!
空白-键 2011-06-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;
}

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧