2017-06-18 20 views
0

所以我使用凌空從數據庫讀取值到我的android應用程序,我有一個問題,當我嘗試從我的數據庫選擇經度和緯度,意圖使用我得到的值響應運行速度比響應實際需要從數據庫獲取選定值的速度快,這會導致始終打開位置爲0,0的Google地圖片段。Android意圖運行速度比響應

這是我的Android代碼

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

回答

0

您沒有將值存儲到第二類的緯度和經度變量中。

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

應該是:

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

intent.getDoubleExtra的工作方式比getStringExtra的工作方式不同。第二個參數是「默認值」。意思是如果意圖沒有找到「緯度」值,它將使用默認值,在這種情況下緯度(自動初始化爲0)。

你可以寫一個任意的值而不是0我寫,並檢查值是否是任意值,然後有意圖的錯誤,所以你做一些特定的事情。當然,這不是一個「必須」,而是一個建議。

+0

idk說什麼男人,謝謝,完美答案:D –

相關問題