在服务端使用谷歌地图地理编码服务
我编写了以下代码来获取地址的坐标
package test;
import java.net.HttpURLConnection; 导入java.net.URL; 导入 sun.net.www.content.text.PlainTextInputStream;
public class a{
public static void main(String[] arg) throws Exception{
String address = "台北市信義路五段七號101樓";
// 查詢經緯度
String output = "csv";
String key = "";
String url = "http://maps.google.com/maps/geo?q=台北市信義路五段七號101樓&output=csv&key=ABQIAAAAXDq__hWKi9eMCwnn7LrMCxT2yXp_ZAY8_ufC3CFXhHIE1NvwkxSnSVp_Xlsd4Ph5iyMua7PE5E0x_A";
URL iurl = new URL(url);
HttpURLConnection uc = (HttpURLConnection)iurl.openConnection();
uc.connect();
Object content = uc.getContent();
// 讀取結果
PlainTextInputStream sr = (PlainTextInputStream)content;
byte[] buf = new byte[2000];
// 解析 200,8,25.033408,121.564099 (HTTP status code, accuracy, latitude, longitude)
sr.read(buf);
String[] tmpArray = new String(buf, "UTF-8").split(",");
String latitude = tmpArray[2];
String longitude = tmpArray[3];
}
}
问题是我在结果中得到了 400 代码的内容 我把网址输入浏览器,它返回 200。
有没有办法在无浏览器的情况下做到这一点?
i wrote the following code to get the coordinate of a address
package test;
import java.net.HttpURLConnection;
import java.net.URL;
import sun.net.www.content.text.PlainTextInputStream;
public class a{
public static void main(String[] arg) throws Exception{
String address = "台北市信義路五段七號101樓";
// 查詢經緯度
String output = "csv";
String key = "";
String url = "http://maps.google.com/maps/geo?q=台北市信義路五段七號101樓&output=csv&key=ABQIAAAAXDq__hWKi9eMCwnn7LrMCxT2yXp_ZAY8_ufC3CFXhHIE1NvwkxSnSVp_Xlsd4Ph5iyMua7PE5E0x_A";
URL iurl = new URL(url);
HttpURLConnection uc = (HttpURLConnection)iurl.openConnection();
uc.connect();
Object content = uc.getContent();
// 讀取結果
PlainTextInputStream sr = (PlainTextInputStream)content;
byte[] buf = new byte[2000];
// 解析 200,8,25.033408,121.564099 (HTTP status code, accuracy, latitude, longitude)
sr.read(buf);
String[] tmpArray = new String(buf, "UTF-8").split(",");
String latitude = tmpArray[2];
String longitude = tmpArray[3];
}
}
The problem is that the content i got a 400 code in result
i put the url in the browser, it return a 200 instead.
Is there a way to do that in a none browser matter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你尝试过 Google Geo Kit 吗
http://gglgeo.codeplex.com/?它也是用Java编写的。
have u tried Google Geo Kit
http://gglgeo.codeplex.com/? it is also in Java.