/**
* 根据地址返回经纬度
* @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
}
R中的画地图的方法不外乎两种,一种是利用GIS方面的数据,在R中画出来,另一种是直接从谷歌地图等地方拿来主义。先说第一种,最早应该是从maps包开始的,这个包里没记错的话应该用map函数为主,
>map("world", fill = TRUE, col = rainbow(200),ylim = c(-90, 90), mar = c(0, 2, 0, 0))
>title("worldmap")
这种用法大家应该很熟了,比较可惜的是里面的数据是在是太少了,连张中国地图都画不了,好在后来有了mapdata等一系列的包,CRAN上maps包后面那一串全是。
具体的内容看一下文档就ok啦,可是问题又来了,R包里的数据总是不够用的,而且还不新,这个时候就可以考虑sp包了,包里的spplot函数可以用来画地图