代码之家  ›  专栏  ›  技术社区  ›  husen

生命周期-Android Studio

  •  -2
  • husen  · 技术社区  · 7 年前

    我创建了一个应用程序,使用谷歌地图api,我的应用程序可以跟踪用户的位置,我想做的是当我打开地图屏幕与按钮点击,我的应用程序跟踪用户的位置,但需要一些时间来跟踪用户依赖于连接对吗?因此,当我的应用程序已经跟踪用户的位置,它会出现一个标记。如果我改变地图屏幕到其他屏幕,然后我回到地图屏幕,它仍然显示没有跟踪标记再次或运行 track function .

    这是我的 跟踪功能

    locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
               double latitude = location.getLatitude();
                double longitude = location.getLongitude();
    
                LatLng latLng = new LatLng(latitude, longitude);
                if(currLocationMarker != null){
                    currLocationMarker.remove();
    
                }
                Geocoder geocoder = new Geocoder(getApplicationContext());
                try {
                    List<Address> addressList = geocoder.getFromLocation(latitude, longitude, 1);
                    String str = addressList.get(0).getSubLocality()+", ";
                    //str += addressList.get(0).getCountryName();
                    str += addressList.get(0).getLocality();
                    currLocationMarker = mMap.addMarker(new MarkerOptions().position(latLng).title(str));
                    lokasisaya.setText(str);
    
                    mMap.addCircle(new CircleOptions()
                            .center(latLng)
                            .radius(10000)
                            .strokeWidth(5)
                            .strokeColor(0x550000FF));
    
    
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
                CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(latLng.latitude, latLng.longitude)).zoom(15).build();
                mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    
            }
    
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
    
            }
    
            @Override
            public void onProviderEnabled(String provider) {
    
            }
    
            @Override
            public void onProviderDisabled(String provider) {
    
            }
        };
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Barns    7 年前

    好吧,你正在移除最后一个 Marker currLocationMarker.remove(); 所以我假设你只需要保持最后一个位置。

    SharedPreferences 要存储最后一个位置,然后在再次启动活动时,请选中以查看该位置是否在中可用 共享引用 .