一切似乎都运行良好。它可以找到我的位置,但不会调用 onLocationChanged() 方法并为我创建标记。有任何想法吗?
public class MainActivity extends FragmentActivity implements LocationListener
{
Context context = this;
GoogleMap googlemap;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initMap();
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
String provider = lm.getBestProvider(new Criteria(), true);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100, 0, this);
}
public void onLocationChanged(Location location) {
LatLng current = new LatLng(location.getLatitude(), location.getLatitude());
Date date = new Date();
googlemap.addMarker(new MarkerOptions()
.title("Current Pos")
.snippet(new Timestamp(date.getTime()).toString())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
.position(current)
);
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
private void initMap(){
SupportMapFragment mf = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
googlemap = mf.getMap();
googlemap.setMyLocationEnabled(true);
googlemap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
}