【问题标题】:Cannot Remove Polyine in googlemap fragment无法删除谷歌地图片段中的折线
【发布时间】:2021-08-26 01:43:03
【问题描述】:

这是用来制作折线的

for (int j = 0; j < user.length(); j++) {
                        JSONObject routes = user.getJSONObject(j);
                        JSONArray rute = routes.getJSONArray("points");

                        PolylineOptions options = new PolylineOptions();
                        for (int k = 0; k < rute.length(); k++) {
                            JSONObject users = rute.getJSONObject(k);
                            double rutes = users.getDouble("latitude");
                            double rutess = users.getDouble("longitude");
                            options.color(Color.parseColor("#FFA866"));
                            options.width(10);
                            options.geodesic(true);
                            options.add(new LatLng(rutes, rutess));

                            Log.d("TAG", "Hasil latitude = " + rutes);
                            Log.d("TAG", "Hasil longtitude = " + rutess);
                            jumlah_klik = jumlah_klik + 1;
                        }
                        polyline_bis = mMap.addPolyline(options);
                    }

这是用于删除折线

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                if (polyline_bis != null){
                    polyline_bis.remove();
                }else {
                    Log.d("TAG","not removee");
                }
                return true;
            }
        });

这是初始化地图片段

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        mapView = mapFragment.getView();

        mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(SearchDanBisKlik.this);
        Places.initialize(SearchDanBisKlik.this, getString(R.string.api_key));
        placesClient = Places.createClient(this);

这是在 GoogleMap googleMap 初始化

mMap = googleMap;

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        mMap.setMyLocationEnabled(true);
        mMap.getUiSettings().setMyLocationButtonEnabled(true);

问题是: 单击标记时无法删除折线,如何删除?帮帮我,谢谢。

【问题讨论】:

    标签: android android-studio google-maps maps


    【解决方案1】:

    多段线添加了几次(user.length()),但对多段线的引用存储在同一个变量polyline_bis

    for (int j = 0; j < user.length(); j++) {
        ...
        polyline_bis = mMap.addPolyline(options);
    }
    

    在这种情况下,polyline_bis 仅处理对最后添加的折线对象的引用。 可能您需要将添加的多段线存储到List 中,并且每次单击标记时选择适当的标记对象。

    【讨论】:

    • @Noerrachmat- 请添加更多代码,例如其中polyline_bis 声明,标记创建等。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 2016-07-07
    • 1970-01-01
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    相关资源
    最近更新 更多