2015-02-08 50 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