地图上没有绘制折线



我想在地图上画一条折线来跟踪我的位置。出于某种原因,我不确定为什么我的方法onMyLocationChanged(位置)没有被调用,也没有在地图上绘制折线。这是与lcoationToLatLng(loc)方法一起使用的方法,该方法可以帮助在地图上绘制跟踪我去的地方的折线。为什么这条折线没有画出来?下面是我的代码:

public class RouteTracking extends FragmentActivity implements View.OnClickListener, OnMyLocationChangeListener{
    private GoogleMap mMap = null;
    private Context context = this;
    private Location lastLocationloc = null;
    @Override
protected void onCreate(Bundle load) { 
    super.onCreate(load);
    setContentView(R.layout.fragment_ride_tracking);
    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.myMapView)).getMap();
    int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
     //Get the current location
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);
    Location location = locationManager.getLastKnownLocation(provider);
    //Zooms into the current location when the activity is started
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    float zoom = (float) 10.0;
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), zoom));      

    if (mMap != null) { 
        mMap.setMyLocationEnabled(true);
    }
   }
   @Override
   public void onMyLocationChange(Location location) { 
       if (lastLocationloc == null) { 
           lastLocationloc = location;
       }
       LatLng lastLatLng = locationToLatLng(lastLocationloc);
       LatLng thisLatLng = locationToLatLng(location);
       mMap.addPolyline(new PolylineOptions().add(lastLatLng).add(thisLatLng).width(3).color(Color.RED));
       lastLocationloc = location;
   }
   public static LatLng locationToLatLng(Location loc) { 
       if (loc != null) 
           return new LatLng(loc.getLatitude(), loc.getLongitude());
       return null;
   }
}

我认为你应该实现LocationListener (implements LocationListener)),然后修改代码为:

   @Override
   public void onLocationChanged(Location location) { 
       if (lastLocationloc == null) { 
           lastLocationloc = location;
       }
       LatLng lastLatLng = locationToLatLng(lastLocationloc);
       LatLng thisLatLng = locationToLatLng(location);
       mMap.addPolyline(new PolylineOptions().add(lastLatLng).add(thisLatLng).width(3).color(Color.RED));
       lastLocationloc = location;
   }

试一试,我希望这能解决你的问题,祝你好运^^

您需要从位置管理器请求(注册)更新(或者至少我没有看到您在哪里这样做)。

    public void init(Context context)
    {
         this.context = context;
         locationManager = (LocationManager) this.context
            .getSystemService(Context.LOCATION_SERVICE);
         // exceptions will be thrown if gps provider is not permitted.
        try
        {
             gpsEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception e)
        {
             Log.d("Fail", e.getMessage());
        }
                // ask for gps updates from the device if gps is enabled
        if (gpsEnabled)
        {
           locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,this);
        }
    }

相关内容

  • 没有找到相关文章

最新更新