我找到两个位置之间的道路距离。我在模拟器上获得正确的输出。但是当我在设备上执行代码时,它刚刚打开应用程序后它的给力关闭。 我不明白为什么会发生这种情况。
public class MainActivity extends Activity implements LocationListener,OnClickListener{
private GoogleMap mGoogleMap;
private Spinner mSprPlaceType;
private String[] mPlaceType=null;
private double mLatitude=0;
private double mLongitude=0;
private LatLng currentLatLng;
private Context mContext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext=this;
Toast.makeText(mContext, "oncreate..",Toast.LENGTH_SHORT).show();
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
String str=GetRoutDistane(mLaitude, mLongitude, END_LATITUDE,END_LONGITUDE);
Toast.makeText(mContext, "distance..."+str,Toast.LENGTH_SHORT).show();
}
public String GetRoutDistane(double startLat, double startLong, double endLat, double endLong)
{
String Distance = "error";
String Status = "error";
try {
parser_Json PJ=new parser_Json(mContext);
JSONObject jsonObj = parser_Json.getJSONfromURL("http://maps.googleapis.com/maps/api/directions/json?origin="+startLat+","+startLong+"&destination="+endLat+","+endLong+"&sensor=true");
Status = jsonObj.getString("status");
Toast.makeText(mContext, "status.."+Status,Toast.LENGTH_SHORT).show();
if(Status.equalsIgnoreCase("OK"))
{
JSONArray routes = jsonObj.getJSONArray("routes");
JSONObject zero = routes.getJSONObject(0);
JSONArray legs = zero.getJSONArray("legs");
JSONObject zero2 = legs.getJSONObject(0);
JSONObject dist = zero2.getJSONObject("distance");
Distance = dist.getString("text");
}
else
{
Distance = "Too Far";
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Distance;
}
@Override
public void onLocationChanged(Location location) {
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
currentLatLng = new LatLng(mLatitude, mLongitude);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onClick(View v) {
Button b = (Button)v;
Toast.makeText(MainActivity.this, b.getText() + " Clicked", Toast.LENGTH_SHORT).show();// TODO Auto-generated method stub
}
}
xml文件 -
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="get distance between two locations" />
答案 0 :(得分:0)
你是否尝试找到2分之间的距离,这些距离是以几分钟或几米的间隔记录的?
跟踪行进路径,而不是计算起点和终点之间的距离,计算中间点的距离,距离之和是最终距离。
就像你想从浦那到孟买旅行一样,你每隔几分钟就会继续保存位置并计算中间距离......
编辑我假设您正在路上旅行,而不仅仅是直接计算。