我找到兩個location.i'm之間的道路距離獲取正確的輸出在emulator.but當我在設備上執行代碼後它剛剛打開應用程序其賦予的力量接近。 我不明白爲什麼這是爲設備發生的。獲取兩個位置之間的距離(道路距離)在Android不工作在真正的設備上
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" />
你從哪裏得到parser_Json?! – Pedrum