4

每次位置更改时,我都想在地图上放置一个标记。我正在从 中获取 Lat 和 Long并在该方法中Location创建一个标记。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();
    addTwittertoMap();

    LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0,
            this);
    String provider = lm.getBestProvider(new Criteria(), true);

}

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) {
}

public void onProviderDisabled(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);
}
4

1 回答 1

2

LatLng current = new LatLng(location.getLatitude(), location.getLatitude());应该 LatLng current = new LatLng(location.getLatitude(), location.getLongitude());

另外,作为对 LocationManager 配置的改进,我建议这样设置:

    LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    String provider = lm.getBestProvider(new Criteria(), true);
    lm.requestLocationUpdates(provider, 10000, 0, this);
于 2013-02-17T01:00:29.500 回答