无法获得“mylocation”Google Maps Android

时间:2013-02-06 11:40:26

标签: android google-maps

我正在尝试获取“蓝点”我的位置坐标并将其设置为我初始mapview加载的中心。但我得到一个空指针异常。

我在这里缺少什么?

public class GetLocationActivity extends MapActivity implements OnClickListener {
private MapView location;
private LocationMarker locationMarker;
private MyLocationOverlay locationOverlay;
private GeoPoint destination;
private GeoPoint source;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.location);

    location = (MapView) this.findViewById(R.id.location);
    location.setBuiltInZoomControls(true);
    location.setClickable(true);
    location.setLongClickable(true);
    location.getController().setZoom(12);
    locationMarker = new LocationMarker(this, location, marker,
            (ImageView) findViewById(R.id.drag));
    location.getOverlays().add(locationMarker);
    locationOverlay = new MyLocationOverlay(this, location);
    location.getOverlays().add(locationOverlay);
    source = locationOverlay.getMyLocation();
    location.getController().setCenter(
            new GeoPoint(source.getLatitudeE6(), source.getLongitudeE6()));
}

source.getLatitudeE6()获取nullPointer。

1 个答案:

答案 0 :(得分:0)

忘记我的基础知识:)。我需要使用LocationManager获取我的位置。

lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
            (LocationListener) this);