2016-05-06 72 views
-1

在項目中,我要跟蹤通過谷歌地圖的任何車輛。在這種情況下,我必須在1分鐘後更新標記位置。但我的問題是,每次谷歌地圖刷新。這裏是我的代碼:如何在不重裝android的情況下更新google地圖?

public class MapsActivity extends FragmentActivity { 

private GoogleMap mMap; 

String json_string; 
JSONObject mJSONObject; 
JSONArray mJSONArray; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 

    json_string = getIntent().getExtras().getString("json_data"); 
    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
      .getMap(); 


    try { 
     mJSONObject = new JSONObject(json_string); 
     mJSONArray = mJSONObject.getJSONArray("server_response"); 

     String lat,lon,imei; 

     for(int i=0;i<mJSONArray.length();i++) { 
      JSONObject jo = mJSONArray.getJSONObject(i); 
      imei = jo.optString("IMEI"); 
      lat = jo.optString("Lattitude"); 
      lon = jo.optString("Longitude"); 

      setUpMapIfNeeded(imei,lat,lon); 

     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 


    } 

private void setUpMapIfNeeded(String imei, String lat, String lon) { 

     if (mMap != null) { 
      double currentLatitude = Double.parseDouble(lat); 
      double currentLongitude = Double.parseDouble(lon); 
      LatLng latLng = new LatLng(currentLatitude, currentLongitude); 
      CameraPosition cameraPosition = new CameraPosition.Builder().target(
        new LatLng(currentLatitude, currentLongitude)).zoom(18).build(); 
      MarkerOptions options = new MarkerOptions() 
        .position(latLng) 
        .title(imei); 
      mMap.addMarker(options); 
      mMap.getUiSettings().setMyLocationButtonEnabled(true); 
      mMap.getUiSettings().setZoomGesturesEnabled(true); 
      mMap.getUiSettings().setZoomControlsEnabled(true); 
      mMap.setBuildingsEnabled(false); 
      mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

    } 
} 

}

請幫助我,因爲我想不獲取地圖重新加載每個標記得到了更新的時間。

回答

0

我猜你需要指定圖標:

MarkerOptions options = new MarkerOptions() 
       .position(latLng) 
       .icon(R.drawable.pin_stop) 
       .title(imei); 
     mMap.addMarker(options); 
1

使用事件也許是做這種事情最簡單的方法。

基本上,添加庫像EventBus,每次收到一個新的位置數據,您的onEvent()內方法,更新您的標記在地圖上的位置。

最簡單的方法是先清除地圖然後添加標記喜歡它是第一次。你可以有一個私人的方法,你可以通過新的緯度和經度然後讓它相應地更新地圖!

我希望這給你如何解決你的問題的想法!

編輯

我認爲(鬆散的),您使用的搖籃構建系統。現在,你的內部文件的build.gradle:在你的依賴條款補充一點:

compile 'org.greenrobot:eventbus:3.0.0' 

然後,創建一個簡單的Java類是這樣的:

public class VehicleLocationUpdatedEvent(){ 

    public void isLocationUpdated; 

    public VehicleLocationUpdatedEvent(boolean updated){ 
     this.isLocationUpdated = updated; 
    } 
} 

接下來,在你的地圖活動或片段,你需要添加以下代碼以偵聽應用程序的其他部分的更新 - 尤其是在收到新的位置數據時。

//inside onCreate ..... 
EventBus.getDefault().register(this); 

//inside onDestroy 
EventBus.getDefault().unregister(this); 


//inside the activity, like any other method add this: 
public void onEvent(VehicleLocationUpdatedEvent event){ 

    if(event.isLocationUpdated){ 

     //call your method that actually updates the marker position 
     //this could simply be animating the camera to the new position 
     //like I said, clear the map of the old marker if you want 
    } 
} 

我們實際使用此事件,您需要在您收到新的位置數據後它 - 就像經度和緯度。您甚至可以修改Event類以包含緯度和經度值,以便您可以方便地使用它們。

//to call the post method, simply do this 
EventBus.getDefault().post(new VehicleLocationUpdatedEvent(true)); 

這就是你需要在Android中使用事件的全部!我希望這有幫助。

+0

你能舉個例子嗎? –

+0

檢查我更新的代碼 – Eenvincible

+0

誰下了票,請添加評論給出一個理由 – Eenvincible

相關問題