简体   繁体   English

如何在具有时间距离的android Map中找到两点之间的路径?

[英]How to find path between two points In android Map with time distance?

I am new bie to android development,some how I am able to load googlemap in android using MapView ,我是 android 开发的新手,我知道如何使用 MapView 在 android 中加载 googlemap

Now I want to find distance between two geopoints .现在我想找到两个 geopoints 之间的距离

For that I found that it can be done为此,我发现它可以做到

using json API (Directions Service )使用 json API(路线服务)

but not able to get url from Api ,which is the best approach to draw path between two places?( including the time distance between places).....无法从 Api 获取 url ,这是在两个地方之间绘制路径的最佳方法?(包括地方之间的时间距离).....

I am following this link as reference http://javapapers.com/android/draw-path-on-google-maps-android-api/ but its not working for me ...我正在关注此链接作为参考http://javapapers.com/android/draw-path-on-google-maps-android-api/但它对我不起作用......

First, you can find straight distance using:首先,您可以使用以下方法找到直线距离:

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);

Second, in order to find the time it takes, I would suggest using Google Places API .其次,为了找到所需的时间,我建议使用Google Places API Super easy to use and free超级易于使用且免费

EDIT: Additional details for google places api (which will indeed give you a poly line which you can easily overlay onto your map)编辑:谷歌地点 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;
    }
}

Polyline code:折线代码:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM