2015-02-08 49 views
0

公共類MainActivity擴展FragmentActivity {啓動地圖當前位置

private GoogleMap mMap; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    setUpMapIfNeeded(); 
} 

private void setUpMapIfNeeded() { 
    if (mMap == null) { 
     // Try to obtain the map from the SupportMapFragment. 
     mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 
     // Check if we were successful in obtaining the map. 
     if (mMap != null) { 
      setUpMap(); 
     } 
    } 
} 
private void setUpMap() { 
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 
    //mMap.setMyLocationEnabled(true); 
} 

}

這是我的活動。 我想用我的當前位置啓動我的應用程序。我查找了一些代碼,但沒有我想要的答案。有人能告訴我我應該補充什麼嗎?

回答

0

你嘗試:

LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10); 
    map.animateCamera(cameraUpdate); 
+0

如何初始化變量'位置'? @patrick – 2015-02-08 12:38:32

+0

您可以: 位置location = new Location(「」); location.setLatitude(0.0d); location.setLongitude(0.0d); 或者像您爲標記添加位置一樣: 示例:new LatLng(0,0)(對於Lat = 0和Lng = 0) – patrick 2015-02-08 18:31:29