如何计算android map中的2个位置之间的距离&输出必须显示在textview或任何?
答案 0 :(得分:3)
我认为你需要googleapi,我之前使用过这样的服务.. 您可以从当前位置到目的地的距离。
只需跟随网址:
这里,origin =%f,%f = origin =纬度,经度 destination =%f,%f = destination =纬度,经度
谷歌回复:
{
"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"
}
上面你可以看到
"distance": {
"text": "1 m",
"value": 0
},
你的距离是:
代码可能如下所示:
private void getDistance(){
StringBuffer jsonString = new StringBuffer();
httpPost = new HttpPost("http://maps.googleapis.com/maps/api/directions/json?origin=<latitude>,<longitude>&destination=<latitude>,<longitude>&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();
}
}
希望它有所帮助。
答案 1 :(得分:2)
textView.setText(""+location1.distanceTo(location2));
答案 2 :(得分:1)
/**
* Calculate the distance between 2 points based on their GeoPoint coordinates. <br>
* Return the value in Km or miles based on the unit input
* @param gp1 (GeoPoint): First point.
* @param gp2 (GeoPoint): Second point.
* @param unit (char): Unit of measurement: 'm' for miles and 'k' for Km.
* @return (double): The distance in miles or Km.
*/
public static double getDistance(GeoPoint gp1, GeoPoint gp2, char unit)
{
//Convert from degrees to radians
final double d2r = Math.PI / 180.0;
//Change lat and lon from GeoPoint E6 format
final double lat1 = gp1.getLatitudeE6() / 1E6;
final double lat2 = gp2.getLatitudeE6() / 1E6;
final double lon1 = gp1.getLongitudeE6() / 1E6;
final double lon2 = gp2.getLongitudeE6() / 1E6;
//The difference between latitudes and longitudes
double dLat = Math.abs(lat1 - lat2) * d2r;
double dLon = Math.abs(lon1 - lon2) * d2r;
double a = Math.pow(Math.sin(dLat / 2.0), 2)
+ Math.cos(lat1 * d2r) * Math.cos(lat2 * d2r)
* Math.pow(Math.sin(dLon / 2.0), 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
//Return the distance
return (unit == 'm' ? 3956 : 6367) * c;
} //End getDistance()
TextView textView.setText("" + getDistance(gp1, gp2, 'k'));
答案 3 :(得分:1)
对于像我这样的人,他们现在/已经学习了。已经使用了下面@NaserShaikh编写的代码。 在这里你去一个工作代码,以获得谷歌地图上的驾驶距离,当你有拉,长两点!距离是英里! 有更好的方法来解析JSON响应请寻找它。 当有海洋btw和其他东西时,没有测试它的极端。希望它有所帮助。
package havefun;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.JSONArray;
import org.json.JSONObject;
class Distance {
private Double dist;
public Distance() {
dist = (double) -1;
}
private Double getDistance(){
StringBuffer jsonString = new StringBuffer();
HttpPost httpPost = new HttpPost("http://maps.googleapis.com/maps/api/directions/json?origin=<src_lat>,<src_long>&destination=<dst_lat>,<dst_long>&sensor=false&mode=driving");
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
try {
CloseableHttpResponse 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());
//System.out.println(jsonObject);
//System.out.println("========================");
//System.out.println(jsonObject.get("status"));
if (jsonObject.get("status").toString().equals("OK") == false) {
System.err.println("Error");
return dist;
}
String distance = jsonObject.getJSONArray("routes").getJSONObject(0).getJSONArray("legs").getJSONObject(0).getJSONObject("distance").getString("text");
System.out.println(distance);
String temp[] = distance.split(" ");
System.out.println(temp[1]);
System.out.println(temp[0]);
dist = Double.parseDouble(temp[0]);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dist;
}
public static void main(String[] args) {
Distance d = new Distance();
Double dist = d.getDistance();
System.out.println("Distance ="+dist);
}
}