当前位置: 首页 > 知识库问答 >
问题:

谷歌地图:如何在恢复地图活动时恢复标记

邵羽
2023-03-14

我在做一个从传感器收集数据的应用程序。每次传感器发送数据时,地图上都会放置一个新的标记。当我按后退,然后恢复地图活动,所有的标记都消失了。在恢复活动时,是否有方法保存和恢复每个唯一的标记及其唯一的标记选项(title、snippet、LatLng)?

 public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener
{

private GoogleMap mMap;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
Marker mCurrLocationMarker;
private int co_mV;
private int no2_mV;
LocationRequest mLocationRequest;
private boolean initiateApp;
String currentTime;

TcpClient mTcpClient;
ArrayList<Marker> markerArrayList;

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

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        checkLocationPermission();
    }
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    initiateApp = true;
    markerArrayList = new ArrayList<Marker>();

}

/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    mMap.setMyLocationEnabled(true);


    if(mGoogleApiClient == null) {
    //Initialize Google Play Services
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            buildGoogleApiClient();

        }
    }
    else {
            buildGoogleApiClient();
        }
    }
}

/* Here we create the infoWindow **/
protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    mGoogleApiClient.connect();

}

@Override
public void onConnected(Bundle bundle) {

    getNewLocation();
    new ConnectTask().execute("");

}


public void newData(JSONObject d) {
    try {
        co_mV = d.getInt("co_mV");
        no2_mV = d.getInt("no2_mV");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    getNewLocation();
}

public void getTime() {

    Calendar cal = Calendar.getInstance();
    currentTime = new SimpleDateFormat("HH:mm:ss").format(cal.getTime());

}

public void getNewLocation() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onLocationChanged(Location location) {

    if (markerArrayList.size()>1) {
        if(location.distanceTo(mLastLocation) < 30) {
            markerArrayList.get(markerArrayList.size()-1).remove();
            markerArrayList.remove(markerArrayList.size()-1);
            Toast.makeText(
                    getApplicationContext(),
                    "Reading to close to last reading, replaces last reading", Toast.LENGTH_SHORT).show();
        }
    }


    if (markerArrayList.size() == 8) {
        markerArrayList.get(0).remove();
        markerArrayList.remove(0);
    }

    //Place current location marker
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());

    if (!initiateApp) {
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.title("Time of reading: " + currentTime);
        markerOptions.snippet("co: " + String.valueOf(co_mV) + " mV, " + "no2: " + String.valueOf(no2_mV) + " mV");
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
        mCurrLocationMarker = mMap.addMarker(markerOptions);
        markerArrayList.add(mCurrLocationMarker);

    }

    mLastLocation = location;


    Log.d("ADebugTag", "Value: " + Double.toString(location.getLatitude()));
    Log.d("ADebugTag", "Value: " + Double.toString(location.getLongitude()));


    //move map camera

    if(initiateApp){
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
    }

    boolean contains = mMap.getProjection()
            .getVisibleRegion()
            .latLngBounds
            .contains(latLng);

    if(!contains){
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    }

    initiateApp = false;
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
public boolean checkLocationPermission(){
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

        // Asking user if explanation is needed
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.ACCESS_FINE_LOCATION)) {

            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

            //Prompt the user once explanation has been shown
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);


        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);
        }
        return false;
    } else {
        return true;
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_LOCATION: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted. Do the
                // contacts-related task you need to do.
                if (ContextCompat.checkSelfPermission(this,
                        Manifest.permission.ACCESS_FINE_LOCATION)
                        == PackageManager.PERMISSION_GRANTED) {

                    if (mGoogleApiClient == null) {
                        buildGoogleApiClient();
                    }
                    mMap.setMyLocationEnabled(true);
                }

            } else {

                // Permission denied, Disable the functionality that depends on this permission.
                Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
            }
            return;
        }

        // other 'case' lines to check for other permissions this app might request.
        // You can add here other case statements according to your requirement.
    }
}

ublic JSONObject getNewJSON(JSONObject json) {
    try {

        int humidity = json.getInt("humidity_ppm");
        int pressure = json.getInt("pressure_Pa");
        int noise = json.getInt("noise_dB");
        double lat = mLastLocation.getLatitude();
        double lng = mLastLocation.getLongitude();
        long time = System.currentTimeMillis() / 1000L;

       JSONObject c = new JSONObject();
        c.put("time",time);
        c.put("lat",lat);
        c.put("long",lng);
        c.put("humidity",humidity);
        c.put("pressure",pressure);
        c.put("noise_dB",noise);
        return c;

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

return null;
}


public class ConnectTask extends AsyncTask<String, String, TcpClient> {

    @Override
    protected TcpClient doInBackground(String... message) {

        //we create a TCPClient object
        mTcpClient = new TcpClient(new TcpClient.OnMessageReceived() {
            @Override
            //here the messageReceived method is implemented
            public void messageReceived(String message) {
                //this method calls the onProgressUpdate
                publishProgress(message);
            }
        });
        mTcpClient.run();

        return null;
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);


        try {
            JSONObject parser = new JSONObject(values[0]);
            JSONObject d = parser.getJSONObject("d");
            JSONObject cloudData = getNewJSON(d);

            if (mLastLocation != null) {
                newData(d);
                getTime();
            }

            System.out.println(cloudData);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //process server response here....
    }

}

共有1个答案

缑桐
2023-03-14

我找到了这个答案,它提供了一个优雅的一行解决方案。当用户旋转手机时,这将恢复标记,但当他按下后退按钮时不会(我知道这不是OP问的,但谷歌会引导人们在这里)

oncreate中添加以下内容:

// mapFragment is a SupportMapFragment 
mapFragment.setRetainInstance(true);
 类似资料:
  • 我成功创建了一个活动,用户可以在其中发布他的状态并将他的当前位置放置在android工作室中创建的google map中。我还尝试在map中查看所有其他用户,我成功地做到了。但是,随着他们的位置发生变化,不断增加。我想实现这样一种情况,即用户在移动时可以在地图上看到彼此,并且这些也会相应地移动。我使用Firebase作为我的后端来存储他们的位置和状态。这是我想出的代码。 请帮帮我!提前感谢!:)

  • 我想更改Google Maps上选定的标记图标,因此我有以下代码: 在这一行,我从下面得到错误: 例外情况如下:

  • 我创建了一个使用提供小型库的活动。但是,我不能让它在活动恢复时刷新(例如,在从其他活动返回后)。每次前两张图片将是空白的,只有当我刷两张图片到旁边,他们得到刷新。我找到的答案都不起作用(特别是重写) 我是这样设置的: 在我试图修复它之后,代码有点乱...但是:删除不起作用。我已经将放在了几个必须调用的地方(如),但没有任何结果。我有点绝望了。

  • 我需要一些帮助来绘制我正在绘制的地图。地图并不特别复杂,因为我是一个初学者,我有一堆带有信息窗口的标记(完成后还会有更多标记),单击标记或选择页面HTML端下拉菜单的相应项时可以打开这些标记。 当信息窗口打开时(在HTML菜单中单击或选择),我想做但自己找不到的是在地图上自动居中标记。我假设有某种函数可以分配给click或infowindow打开事件,但无法确定是哪种函数以及如何实现它。 我的代码

  • 我有MainActivity上有一个片段,当用户点击片段时,用户会导航到另一个片段,在最后一个片段上有一个按钮来启动谷歌地图导航,这是代码: 问题是,在地图启动后,我希望用户在按下back时返回到我的应用程序,而不是Launcher。 有没有办法做到这一点。 这是我的完整代码: 主体活动 原木

  • 我有一个包含3个活动的应用程序。 我有主要活动。这将调用第二个活动,然后调用第三个活动。我想在不进入 onCreate 的情况下返回到主要活动。 这是第三个活动的代码: