Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中更新google地图而不重新加载?_Android - Fatal编程技术网

如何在android中更新google地图而不重新加载?

如何在android中更新google地图而不重新加载?,android,Android,在我的项目中,我必须通过谷歌地图追踪任何车辆。在这种情况下,我必须在我设定的1分钟后更新标记位置。但我的问题是,每次谷歌地图被刷新。这是我的密码: public class MapsActivity extends FragmentActivity { private GoogleMap mMap; String json_string; JSONObject mJSONObject; JSONArray mJSONArray; @Override protected void onCre

在我的项目中,我必须通过谷歌地图追踪任何车辆。在这种情况下,我必须在我设定的1分钟后更新标记位置。但我的问题是,每次谷歌地图被刷新。这是我的密码:

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));

    }
}
公共类映射活动扩展了FragmentActivity{
私有谷歌地图;
字符串json_字符串;
JSONObject mJSONObject;
杰索纳雷;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
json_string=getIntent().getExtras().getString(“json_数据”);
mMap=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
试一试{
mJSONObject=新的JSONObject(json_字符串);
mJSONArray=mJSONObject.getJSONArray(“服务器响应”);
字符串lat、lon、imei;

对于(int i=0;i我想您需要指定以下图标:

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

使用事件可能是做这类事情最简单的方法

基本上,添加一个库,例如,每当您收到新的位置数据时,在您的onEvent()方法中,更新您在地图上的标记位置

最简单的方法是先清除地图,然后像第一次一样添加标记。你可以有一个私有方法,通过新的纬度和经度,然后让它相应地更新地图

我希望这能给你一个解决问题的方法

编辑

我(松散地)假设您使用Gradle构建系统。现在,在build.Gradle文件中:在您的dependencies子句中添加以下内容:

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
    }
}
现在要实际使用此事件,您需要在收到新的位置数据(如纬度和经度)时发布它。您甚至可以修改事件类以包含纬度和经度值,以便方便使用

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

这就是在Android中使用事件所需要的一切!我希望这会有所帮助。

你能举个例子吗?不管是谁投了唐恩的票,请添加一条评论来说明原因我不知道谁投了唐恩的票,但基于事件的解决方案听起来很不错。我认为唐恩的家伙不知道事件,我投了你们的票。也许他只是看看答案的格式,不知道现在stackoverflow有这样的用户:(我编辑了你的答案:当启用即时运行时,有时eventbus没有注册。基本上你可以在每个后期处理之前检查注册状态。