我正在寻找两个位置之间的道路距离。我在模拟器上得到了正确的输出。但是当我在设备上执行代码时,它只是在打开应用程序后才强制关闭。我不明白为什么设备会发生这种情况。
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" />