Прихващане на потребителско местоположение с google maps v2 на android?

Искам да поставя маркер на картата при всяка промяна на местоположението. Грабвам 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);
}

person Jane Doh    schedule 10.02.2013    source източник
comment
сигурни ли сте, че получавате актуализации за местоположение?   -  person iagreen    schedule 10.02.2013
comment
Да, тествах го навън и го карах наоколо и се актуализира.   -  person Jane Doh    schedule 11.02.2013
comment
Бих увеличил мащаба до извлеченото местоположение, само за да проверя. Така че във вашия onLocationChanged, увеличете мащаба до местоположението, за да сте сигурни, че маркерът не е добавен.   -  person Sherif elKhatib    schedule 15.02.2013


Отговори (1)


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);
person Houf    schedule 17.02.2013