2013-02-06 100 views
0

我想獲得「藍點」我的位置座標,並將其設置爲我的初始mapview負載的中心。但是我得到一個空指針異常。無法獲得「mylocation」谷歌地圖Android

我在這裏錯過了什麼?

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())); 
} 

獲得nullPointer source.getLatitudeE6()

+1

您的代碼中沒有位置偵聽器,然後無法獲取當前位置。請閱讀以下文檔:http://developer.android.com/intl/ko/training/basics/location/currentlocation.html – BBonDoo

回答

0

忘記我的基礎在這裏:)。我需要使用LocationManager獲取我的位置。

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