使用Volley从当前位置查找附近地点的距离和时间

时间:2016-04-09 07:07:38

标签: android google-api android-volley

我是android studio的新手。我想使用Volley从Google服务器检索数据。我使用两个HTTP URL来搜索附近的地点以及距离当前位置的距离和时间。但是,我找不到任何使用Volley发送两个“JSONRequest”的方法。第一个请求搜索附近的地点,第二个请求距当前位置的每个附近地点的距离和时间矩阵。距离矩阵HTTP URL取决于第一个在可以执行之前找到附近位置的URL。有没有方法使用单个HTTP URL搜索距离和时间附近的地方?

或者使用Volley进行多项请求的其他方法?

感谢您的帮助。

这里是我正在使用的代码:

private int PROXIMITY_RADIUS = 5000; //5.0KM
private String NAME = "emergency";// emergency+hospital
private String TYPE = "hospital";

private String googlePlacesData;
private String googleDistData;
private Places placeJsonParser = new Places();

public List<PlaceData> searchNearby(double latitude, double longitude)
        throws Exception{

    List<PlaceData> googlePlacesList = new ArrayList<>();

    try {

        StringBuilder googlePlacesUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
        googlePlacesUrl.append("location=" + latitude + "," + longitude);
        //googlePlacesUrl.append("&radius=" + PROXIMITY_RADIUS);
        googlePlacesUrl.append("&rankby=distance");
        googlePlacesUrl.append("&name=" + NAME);
        googlePlacesUrl.append("&type=" + TYPE);
        googlePlacesUrl.append("&sensor=true");
        googlePlacesUrl.append("&key=" + GOOGLE_API_KEY);

        if (googlePlacesUrl.length() > 0)
            System.out.println("Exist GooglePlacesURL: " + googlePlacesUrl.toString());

        Http http = new Http();
        googlePlacesData = http.read(googlePlacesUrl.toString());
        googlePlacesList = placeJsonParser.parse(new JSONObject(googlePlacesData));

    }catch (Exception e){
        e.printStackTrace();
    }

    return googlePlacesList;
}

public List<PlaceData> distanceMatrix(List<PlaceData> googlePlace,GPSTracker gps){

    List<PlaceData> googlePlacesDistTimeList = new ArrayList<>();

    try {

    StringBuilder googleDistTimeURL = new StringBuilder("https://maps.googleapis.com/maps/api/distancematrix/json?");
    googleDistTimeURL.append("origins=" + gps.getLatitude() + "," + gps.getLongitude());
    googleDistTimeURL.append("&destinations=");

    int counter = 0;

    for (PlaceData place:googlePlace){
        if (counter != (googlePlace.size()-1)){
            googleDistTimeURL.append(place.getLatitude() + "," + place.getLongitude() + "|");
        }else {
            googleDistTimeURL.append(place.getLatitude() + "," + place.getLongitude());
        }
        counter++;
    }
    googleDistTimeURL.append("&key=" + GOOGLE_API_KEY);

    if (googleDistTimeURL.length() > 0)
        System.out.println("Exist GooglePlacesURL: " + googleDistTimeURL.toString());

    Http http = new Http();
    googleDistData = http.read(googleDistTimeURL.toString());
    googlePlacesDistTimeList = placeJsonParser.parseDistTime(new JSONObject(googleDistData),googlePlace);

}catch (Exception e){
    Log.e("Exception", e.toString());
}

    return googlePlacesDistTimeList;
}

0 个答案:

没有答案