2016-05-06 173 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

相关问题