【问题标题】:android intent runs faster than responseandroid意图运行速度比响应快
【发布时间】:2017-11-20 22:07:47
【问题描述】:

所以我使用 volley 将值从数据库读取到我的 android 应用程序,当我尝试从我的数据库中选择纬度和经度时遇到问题,使用我从响应中获得的值的意图运行速度比响应实际上需要从数据库中获取选定的值,这导致总是打开一个位置为 0,0 的谷歌地图片段。

这是我的安卓代码

Response.Listener<String> responseListener4 =new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonResponse = new JSONObject(response);
                            boolean success=jsonResponse.getBoolean("success");
                            if (success) {
                                lat= jsonResponse.getDouble("lat");
                                lng= jsonResponse.getDouble("lng");
                                System.out.println(lat);
                                System.out.println(lng);
                                Intent i2=new Intent(lists_activity.this,supervisor_maps.class);
                                i2.putExtra("lat",lat);
                                i2.putExtra("lng",lng);
                                startActivity(i2);
                            } else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(lists_activity.this);
                                builder.setMessage("Loading Trucks Failed...")
                                        .setNegativeButton("Retry",null)
                                        .create()
                                        .show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };
                UpdatedLocation updatedLocation=new UpdatedLocation(cmp,splitted[1],responseListener4);
                RequestQueue queue=Volley.newRequestQueue(lists_activity.this);
                queue.add(updatedLocation);

这是我应该去的类并从片段中打开位置。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(GoogleServiceAvailable())
    {
        Toast.makeText(this,"Perfect!!!",Toast.LENGTH_LONG).show();
        setContentView(R.layout.activity_driver_maps);
        Intent test=getIntent();
        test.getDoubleExtra("lat",latitude);
        test.getDoubleExtra("lng",longitude);
        System.out.println(latitude);
        System.out.println(longitude);
        initMap();
    }
    else
    {
        setContentView(R.layout.error);
    }
}
public boolean GoogleServiceAvailable()
{
    GoogleApiAvailability api=GoogleApiAvailability.getInstance();
    int isAvailable=api.isGooglePlayServicesAvailable(this);
    if (isAvailable== ConnectionResult.SUCCESS)
    {
        return true;
    }
    else if (api.isUserResolvableError(isAvailable))
    {
        Dialog dialog=api.getErrorDialog(this,isAvailable,0);
        dialog.show();
    }
    else
    {
        Toast.makeText(this,"Cant connect to play services",Toast.LENGTH_LONG).show();
    }
    return false;
}
private void initMap() {
    MapFragment mapFragment= (MapFragment) getFragmentManager().findFragmentById(R.id.mapFragment);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mGoogleMap=googleMap;
    goToLocation(latitude, longitude,15);
}

private void goToLocation(double lat, double lng, float zoom) {
    LatLng ll=new LatLng(lat,lng);
    CameraUpdate update= CameraUpdateFactory.newLatLngZoom(ll,zoom);
    mGoogleMap.moveCamera(update);
    mGoogleMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude)).title("Your Location."));
}

}

当然,您可以猜到前 2 个 system.out 打印位置,在本例中是 30,30 个测试值,但第二个类中的第二个 system.out 仅打印 0,0

【问题讨论】:

    标签: java php android google-maps android-studio


    【解决方案1】:

    您没有将值存储到第二类的纬度和经度变量中。

    test.getDoubleExtra("lat",latitude);
    test.getDoubleExtra("lng",longitude);
    

    应该是:

    latitude = test.getDoubleExtra("lat",0);
    longitude = test.getDoubleExtra("lng",0);
    

    intent.getDoubleExtra 的工作方式与 getStringExtra 的工作方式不同。第二个参数是“默认值”。这意味着如果意图没有找到“纬度”值,它将使用默认值,在这种情况下纬度(自动初始化为 0)。

    您可以写一个任意值而不是我写的 0,并检查该值是否是该任意值,然后意图出现错误,因此您可以执行特定的操作。当然,这不是“必须”而是建议。

    【讨论】:

      猜你喜欢
      • 2015-10-27
      • 2015-09-23
      • 1970-01-01
      • 1970-01-01
      • 2011-08-27
      • 2012-03-29
      • 2012-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多