【问题标题】:Can't Remove Polyline on Google Map (Android)无法删除谷歌地图上的折线(Android)
【发布时间】:2016-03-22 14:35:21
【问题描述】:

我正在创建应用程序来跟踪我的步数。我使用谷歌地图 API(V2)。我边走边画步(实时) 这是我的代码。

    case R.id.btn_location:
            if(polyline != null) {
                polyline.remove();
            }
            if(mStatus == IDLE) {
                map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                        .getMap();
                getmyLocation.getmyLocation();
                map.clear();
            }
             break;
case R.id.btn_can_location:

            if (mStatus == RUNNING) {
                mTimer.removeMessages(0);
                mPauseTime = SystemClock.elapsedRealtime();
                mStatus = IDLE;
                loc_manager.removeUpdates(listener);
             break;

//--------------------------------------------- --------------------------------------------------/

public class getMyLocation {
  private void getmyLocation() {
        if (loc_manager == null)
            loc_manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        long time = 3000; 

        float minDistance = 10;

        loc_manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, time, minDistance, listener);
        }
    }


private void getmyLocation() {
        if (loc_manager == null)
            loc_manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        long time = 3000; 

        float minDistance = 10;

        loc_manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, time, minDistance, listener);

static class MyLocationManager implements LocationListener {
    public void onLocationChanged(Location location) {
     polyline = map.addPolyline(polylineOptions.add(new LatLng(location.getLatitude(), location.getLongitude())));
        polylineOptions.width(5);
        polylineOptions.color(Color.RED);

        map.addPolyline(polylineOptions);
}

实时绘制路线很好。但是删除折线不起作用。 帮帮我

【问题讨论】:

  • 您可以使用 'googlemap.clear();'清除谷歌地图
  • 在尝试删除折线时,logcat 中是否显示任何异常或错误?
  • 是的。 Logcat 很清楚。没有发生错误或异常。

标签: android google-maps polyline


【解决方案1】:

我认为你的问题出在这方面。

case R.id.btn_location:
            if(polyline != null) {
                polyline.remove();
            }
            if(mStatus == IDLE) {
                map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                        .getMap();
                getmyLocation.getmyLocation();
                map.clear();
            }
             break;

尝试像这样初始化地图

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

你还需要实现OnMapReadyCallback,然后像这样在地图中做任何你想做的事情

@Override
public void onMapReady(GoogleMap googleMap) {

    Double lat = Double.parseDouble(latt); // latt is string
    Double lang = Double.parseDouble(lngg); // langg is string
    LatLng location = new LatLng(lat, lang);
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(location, 16);
    googleMap.addMarker(new MarkerOptions().position(location).title(""));
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(location));
    googleMap.animateCamera(cameraUpdate);
}

并在 xml 文件中添加这一行。

<fragment
   android:id="@+id/map"
   android:name="com.google.android.gms.maps.SupportMapFragment"
   android:layout_width="match_parent"
   android:layout_height="@dimen/hundred_thirty_dp"/>

1 每次点击按钮时都不会获取地图对象。只是 一开始就初始化一次

2 当您拨打polyline.remove 时,您还需要拨打map.clear();

【讨论】:

  • 哦,谢谢你的回答。我在 onCreate 中初始化地图,地图是静态的。 map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);有问题吗...?
  • 感谢您的回答。我尝试更改我的代码。谢谢
  • 可悲的是它不起作用。我意识到折线已被删除!按下按钮时,折线和标记被移除。但是回调 onLocationChanged 会重新绘制折线。 (在绘制之前..)所以折线混淆了哪个是第一个。我认为 LatLng 是在 buttonDown 上初始化的。但是我该怎么办?
  • 现在这是另一个问题,请解释您想要什么。并且我会更新答案。
  • 啊.. 完成跟踪步骤后,显示折线。我想删除该行。所以我在“onClick”上使用 polyline.remove() 方法。这是工作。但是,ReTracking 步骤,draing polyline 包括在跟踪点之前。所以看起来移除折线是行不通的。
猜你喜欢
  • 2021-08-26
  • 1970-01-01
  • 2015-07-12
  • 1970-01-01
  • 1970-01-01
  • 2016-07-07
  • 2017-02-06
  • 1970-01-01
  • 2012-08-21
相关资源
最近更新 更多