【问题标题】:getting distance between two locations(road distance) in android not working on real device在android中获取两个位置之间的距离(道路距离)在真实设备上不起作用
【发布时间】:2013-10-06 05:48:19
【问题描述】:

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

  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 的?!

标签: android google-maps-api-3 gps location google-directions-api


【解决方案1】:

您是否尝试找到以几分钟或几米的间隔记录的 2 个点之间的距离?

跟踪经过的路径,而不是计算起点和终点之间的距离,而是计算中间点的距离,距离之和就是你的最终距离。

就像您想从浦那到孟买一样,您每隔几分钟就会保存位置并计算中间距离...

编辑我假设您是在路径上行驶,而不仅仅是直接计算。

【讨论】:

  • 问题不是要计算的距离或位置,而是在模拟器上正常工作,而不是在真实设备上。请再次检查我的问题。
  • @yuva 那么你遇到了什么问题?你的问题有点混乱。
  • @yuva 请粘贴 logcat,以便我们能够确定哪个请求需要时间..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-16
  • 1970-01-01
相关资源
最近更新 更多