我在做什么:
在单击按钮时,我正在启动一个活动,并使它看起来像一个对话框
怎么了:
我能够看到地图,但相机未对准所需的纬度和经度。而且没有错误
ActMap.java
public class ActMap extends FragmentActivity {
private static double latitude=0.0,longitude=0.0;
private View rootView;
Location lastKnownLocationNetwork;
Location lastKnownLocationGPS;
LocationManager locationManager;
GPSTracker gps;
private ProgressDialog prg;
GoogleMap map;
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(new ColorDrawable(0));
setContentView(R.layout.act_map);
gps = new GPSTracker(this);
prg = new ProgressDialog(this);
locationManager = (LocationManager)getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
lastKnownLocationNetwork = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
lastKnownLocationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
this.rootView=findViewById(R.id.map_view);
if(CommonFunctions.isGoogleservicesConnected()){
initilizeMap();
setCamera();
}
/*FragmentManager myFragmentManager = getSupportFragmentManager();
SupportMapFragment mySupportMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
map = mySupportMapFragment.getMap();*/
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
}
@SuppressLint("NewApi")
@Override
public boolean onTouchEvent(MotionEvent event) {
Rect rect = new Rect();
rootView.getHitRect(rect);
if (!rect.contains((int)event.getX(), (int)event.getY())){
setFinishOnTouchOutside(false);
return true;
}
return false;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
finish();
}
return true;
}
private void setCamera() {
Log.d("LATITUDE", AppController.getLatitude()+"");
Log.d("LONGITUDE", AppController.getLongitude()+"");
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(AppController.getLatitude(), AppController.getLongitude())).zoom(16).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
/**
* function to load map. If map is not created it will create it for you
* @throws Exception
* */
private void initilizeMap() {
try {
if (googleMap == null) {
//Map Explicit settings//
//Map Rotate Gesture
googleMap.getUiSettings().setRotateGesturesEnabled(true);
//My Location Button
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
//Compass Functionality
googleMap.getUiSettings().setCompassEnabled(true);
//Zooming Functionality
googleMap.getUiSettings().setZoomGesturesEnabled(true);
//Zooming Buttons
googleMap.getUiSettings().setZoomControlsEnabled(true);
//Showing Current Location
googleMap.setMyLocationEnabled(true);
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(this,"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
}
}
}
日志:
12-22 16:10:56.109: D/dalvikvm(28773): GC_FOR_ALLOC freed 10499K, 44% free 6245K/11036K, paused 26ms, total 26ms
12-22 16:10:56.294: D/dalvikvm(28773): GC_FOR_ALLOC freed 1189K, 36% free 7104K/11036K, paused 24ms, total 24ms
12-22 16:10:56.573: D/dalvikvm(28773): GC_FOR_ALLOC freed 1009K, 27% free 8142K/11036K, paused 33ms, total 33ms
12-22 16:10:56.836: D/dalvikvm(28773): GC_FOR_ALLOC freed 1148K, 19% free 9033K/11036K, paused 44ms, total 44ms
12-22 16:10:57.062: D/Network(28773): Network
12-22 16:10:57.071: D/Location Updates(28773): Google Play services is available.
12-22 16:10:57.072: D/LATITUDE(28773): 12.9569828
12-22 16:10:57.073: D/LONGITUDE(28773): 77.6411645
表现
<uses-permission android:name="android.permission.CAMERA" />
<activity
android:name="com.windhyaworks.activities.ActMap"
android:excludeFromRecents="true"
android:launchMode="singleInstance"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Dialog" >
</activity>
<!-- other codes -->
最佳答案
尝试这个
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(cameraPosition, 18.0f));