通过共享流程保存多个标记

发布于 2025-02-01 12:37:36 字数 1944 浏览 1 评论 0原文

我发现了有关如何在地图片段上添加多个标记的一些TUT。但是就我而言,看来它对我不起作用,我可以放置多个标记,但是每当我重新启动应用程序时,这些通过共享的标记都消失了。

 @Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        buildGoogleApiClient();
        mMap.setMyLocationEnabled(true);
    }

    mMap.setMyLocationEnabled(true);

    prefs = this.getSharedPreferences("location", 0);

    locationCount = prefs.getInt("locationCount", 0);

    if(locationCount != 0){
        String lat = "";
        String lng = "";

        for (int i = 0; i < locationCount; i++) {
            lat = prefs.getString("Lat" + i,"0");
            lng = prefs.getString("Lng" + i, "0");
            drawMaker(new LatLng(Double.parseDouble(lat), Double.parseDouble(lng)));
        }
    }

    mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

        @Override
        public void onMapClick(@NonNull LatLng latLng) {
            locationCount++;

            drawMaker(latLng);

            SharedPreferences.Editor editor = prefs.edit();

            editor.putString("lat" + Integer.toString((locationCount - 1)), Double.toString(latLng.latitude));

            editor.putString("lng" + Integer.toString((locationCount - 1)), Double.toString(latLng.longitude));

            editor.putInt("locationCount", locationCount);

            editor.commit();

            Toast.makeText(MapsActivity.this, "Marker is added to the Map", Toast.LENGTH_SHORT).show();
        }
    });

    mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
        @Override
        public void onMapLongClick(@NonNull LatLng latLng) {
            mMap.clear();

            SharedPreferences.Editor editor = prefs.edit();

            editor.clear();

            editor.commit();

            locationCount = 0;
        }
    });

I have found some tuts on how to do add multiple markers on your map fragment. But in my case, It seems that it doesn't work for me, well I can put multiple markers but whenever I restart the application those markers that was save through sharedpreferences is gone.

 @Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        buildGoogleApiClient();
        mMap.setMyLocationEnabled(true);
    }

    mMap.setMyLocationEnabled(true);

    prefs = this.getSharedPreferences("location", 0);

    locationCount = prefs.getInt("locationCount", 0);

    if(locationCount != 0){
        String lat = "";
        String lng = "";

        for (int i = 0; i < locationCount; i++) {
            lat = prefs.getString("Lat" + i,"0");
            lng = prefs.getString("Lng" + i, "0");
            drawMaker(new LatLng(Double.parseDouble(lat), Double.parseDouble(lng)));
        }
    }

    mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

        @Override
        public void onMapClick(@NonNull LatLng latLng) {
            locationCount++;

            drawMaker(latLng);

            SharedPreferences.Editor editor = prefs.edit();

            editor.putString("lat" + Integer.toString((locationCount - 1)), Double.toString(latLng.latitude));

            editor.putString("lng" + Integer.toString((locationCount - 1)), Double.toString(latLng.longitude));

            editor.putInt("locationCount", locationCount);

            editor.commit();

            Toast.makeText(MapsActivity.this, "Marker is added to the Map", Toast.LENGTH_SHORT).show();
        }
    });

    mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
        @Override
        public void onMapLongClick(@NonNull LatLng latLng) {
            mMap.clear();

            SharedPreferences.Editor editor = prefs.edit();

            editor.clear();

            editor.commit();

            locationCount = 0;
        }
    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文