如何找到两点之间的路径在android地图中的时间距离?

时间:2015-06-17 12:11:48

标签: android google-maps direction

我是Android开发的新手,我可以能够使用MapView 在Android中加载googlemap,

现在我想找到两个地理位置之间的距离

为此我发现它可以完成

  

使用json API(路线服务)

无法从Api 获取网址,这是在两个地方之间绘制路径的最佳方法吗?(包括地点之间的时间距离).....

我关注此链接作为参考http://javapapers.com/android/draw-path-on-google-maps-android-api/,但它不适合我......

1 个答案:

答案 0 :(得分:0)

首先,您可以使用以下方法找到直线距离:

Location loc1 = new Location();
loc1.setLatitude(43.44556);
loc1.setLongitude(88.56567);
Location loc2 = new Location();
loc2.setLatitude(45.44556);
loc2.setLongitude(83.56567);

double distance = loc1.distanceTo(loc2);

其次,为了找到所需的时间,我建议使用Google Places API。超级好用且免费

编辑:google places api的其他详细信息(这确实会为您提供一条折线,您可以轻松地将其叠加到地图上)

static final String OUT_JSON = "/json";
static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api";
/**
 * Given a destination place, this will calculate the disance and time it will take to get there from your current location
 * Documentation: https://developers.google.com/maps/documentation/directions/
 * @param place destination place
 * @return String array with index 0 the distance text, index 1 the duration text, and index 2 being the ETA
 */
private String[] getRouteDet(LatLng place, double currentLat, double currentLon) {
    final String APPENDAGES = "/directions";
    StringBuilder sb = new StringBuilder(PLACES_API_BASE + APPENDAGES + OUT_JSON);
    sb.append("?key=");
    sb.append(getResources().getString(R.string.google_places_key));
    try {
        sb.append("&origin=");
        sb.append(URLEncoder.encode(Double.toString(currentLat),"utf8"));
        sb.append(",");
        sb.append(URLEncoder.encode(Double.toString(currentLon,"utf8"));
        sb.append("&destination=");
        sb.append(URLEncoder.encode(Double.toString(place.latitude),"utf8"));
        sb.append(",");
        sb.append(URLEncoder.encode(Double.toString(place.longitude), "utf8"));
        sb.append("&departure_time=now");
        sb.append("&mode=");
        sb.append("driving");
        URL url = new URL(sb.toString());
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        InputStreamReader in = new InputStreamReader(conn.getInputStream());
        // Load the results into a StringBuilder
        int read;
        char[] buff = new char[1024];
        StringBuilder jsonResults = new StringBuilder();
        while ((read = in.read(buff)) != -1) {
            jsonResults.append(buff, 0, read);
        }
        // Create a JSON object hierarchy from the results
        JSONObject jsonObj = new JSONObject(jsonResults.toString());
        if (jsonObj.getString("status").equals("ZERO_RESULTS")) return null;
        JSONObject result = jsonObj.getJSONArray("routes").getJSONObject(0).getJSONArray("legs")
                .getJSONObject(0);
        String distance = result.getJSONObject("distance").getString("text");
        String duration = result.getJSONObject("duration").getString("text");
        long durationVal = result.getJSONObject("duration").getLong("value");
        DateFormat formatter = new SimpleDateFormat("h:mm a", Locale.US);
        return new String[]{distance, duration, formatter.format(System.currentTimeMillis()+durationVal*1000)};
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

折线代码:

PolylineOptions options = new PolylineOptions().width(15).color(Color.RED).geodesic(true);
//Loop through all of your LatLng steps and add each to the polyline:
//    options.add(step);
map.addPolyline(options);