美好的一天。 我正在开发一个Android应用程序,可以使用一些帮助。
我正在尝试找出在Google地图中搜索特定位置的正确语法。 IE,我位于(lat,lng),想找到所有名为" Walmart"的商店/商店。靠近那些坐标。
我做了一些研究,似乎有多人建议使用" Google Places",但由于Google地图最近发生了巨大变化,我不确定它是否已被弃用。
根据建议更新了代码:
@SuppressWarnings("deprecation")
public double[] searchForStuff(){
final String searchString = "Starbucks";
final double[] placeLocations = new double[2];
Thread backgroundThread = new Thread (new Runnable(){
public void run(){
try{
final URL url = new URL("http://maps.googleapis.com/maps/api/geocode/json?address="
+ URLEncoder.encode(searchString) + "&sensor=true");
URLConnection conn = url.openConnection();
InputStreamReader streamReader = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(streamReader);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
br.close();
JSONObject mainObject = new JSONObject(sb.toString());
JSONObject result = mainObject.getJSONArray("results").getJSONObject(0); // Array of location objects
JSONObject geometry = result.getJSONObject("geometry");
JSONObject location = geometry.getJSONObject("location");
double latitude = location.getDouble("lat");
double longitude = location.getDouble("lng");
placeLocations[0] = latitude;
placeLocations[1] = longitude;
} catch (IOException e) {
String error = e.toString();
} catch (Exception e){
String error = e.toString();
}
}
});//
backgroundThread.start();
Toast.makeText(getActivity(), "Is this working? " + placeLocations[0], Toast.LENGTH_LONG); //Nope :(
return placeLocations;
}
仍然有错误
-Sil
答案 0 :(得分:0)
尝试这种方式:
URL url = new URL("http://maps.googleapis.com/maps/api/geocode/json?address="
+ URLEncoder.encode(address) + "&sensor=true");
// address is a string variable, "Walmart" for example
URLConnection conn = url.openConnection();
InputStreamReader streamReader = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(streamReader);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
br.close();
JSONObject mainObject = new JSONObject(sb.toString());
JSONObject result = mainObject.getJSONArray("results").getJSONObject(0); // Array of location objects
JSONObject geometry = result.getJSONObject("geometry");
JSONObject location = geometry.getJSONObject("location");
double latitude = location.getDouble("lat");
double longitude = location.getDouble("lng");
名为&#34的对象数组;结果"是位置列表。