我正在尝试在地图上绘制一条折线以跟踪我的位置。出于某种原因,我不确定为什么我的方法 onMyLocationChanged(location) 没有被调用并且没有在地图上绘制折线。这是与 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;
}
}