无法在Google地图上删除折线(Android)

时间:2016-03-22 05:54:18

标签: android google-maps polyline

我正在创建用于跟踪我的步骤的应用。我使用谷歌MAP 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);
}

实时绘图路线很好。但删除折线不起作用。 Helpme

1 个答案:

答案 0 :(得分: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();