2013-09-30 85 views
0

我找到兩個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" /> 

+0

你從哪裏得到parser_Json?! – Pedrum

回答

0

你嘗試找出它被記錄在間隔說數分鐘或數米2點之間的距離?

保持行進路徑的軌跡,而不是計算起點和終點之間的距離,計算中間點的距離,距離之和是您的最終距離。

像你想從普納孟買旅行,你繼續保存位置,每隔幾分鐘,並計算中間距離...

編輯我假設你的道路上行駛,而不僅僅是直接計算。

+0

問題不在距離或位置上進行計算,它在仿真器上正常工作,不在真實設備上。請再次通過我的問題。 –

+0

@yuva所以你得到什麼問題?你的問題有點混亂。 –

+0

@yuva請粘貼logcat,以便我們能夠確定哪個請求需要花費時間。 – Pallavi

相關問題