Distance between two location in Android map

后端 未结 4 692
误落风尘
误落风尘 2021-01-07 10:22

How to calculate distance between 2 location in android map & output must display in textview or any ?

4条回答
  •  死守一世寂寞
    2021-01-07 10:52

    i think you need googleapi for that, i had use such service before.. you can get distance from your current location to destination.

    simply uee following url:

    http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=false&mode=driving

    here, origin=%f,%f = origin=latitude,longitude destination=%f,%f = destination=latitude,longitude

    google response:

    {
    
        "routes": [
            {
                "bounds": {
                    "northeast": { … },
                    "southwest": { … }
                },
                "copyrights": "Map data ©2012 Inav/Geosistemas SRL",
                "legs": [
                    {
                        "distance": {
                            "text": "1 m",
                            "value": 0
                        },
                        "duration": { … },
                        "end_address": "Formosa Province, Argentina",
                        "end_location": { … },
                        "start_address": "Formosa Province, Argentina",
                        "start_location": { … },
                        "steps": [ … ],
                        "via_waypoint": [ ]
                    }
                ],
                "overview_polyline": { … },
                "summary": "RP 3",
                "warnings": [ ],
                "waypoint_order": [ ]
            }
        ],
        "status": "OK"
    
    }
    

    Above u can see

    "distance": {
                                "text": "1 m",
                                "value": 0
                            },
    

    there is your distance:

    code may look like this:

    private void getDistance(){
    StringBuffer jsonString = new StringBuffer();
    
            httpPost = new HttpPost("http://maps.googleapis.com/maps/api/directions/json?origin=,&destination=,&sensor=false&mode=driving");
    
    
            try {
                httpPost.setEntity(new UrlEncodedFormEntity(params));
                httpResponse = httpClient.execute(httpPost);
    
                InputStream in = httpResponse.getEntity().getContent();
                int ch = 0;
                while ((ch = in.read()) != -1) {
                    jsonString.append((char) ch);
                }
                in.close();
    
                JSONObject jsonObject = new JSONObject(jsonString.toString());
                JSONArray jsonArray = jsonObject.getJSONArray(""legs");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jObj = jsonArray.getJSONObject(i);
    
                    String text = jObj.getString("text");
                    String value = jObj.getString("value");//value is ur distance
                }
    
    
    
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
    }
    

    hope it help.

提交回复
热议问题