Google Map上的当前位置更新编码无法在Android中使用 [英] Current Location Update coding on google map not working in android
本文介绍了Google Map上的当前位置更新编码无法在Android中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将摄像头缩放到google map android中的当前位置,但是我的代码无法正常工作,我尝试了很多方法,我的gps位置在电话中,此代码可以在模拟器上工作,但无法在设备上工作,我也尝试过onLocationChanged方法请任何伙伴建议我适当的解决方案
I am trying to zoom camera on current location in google map android but my code is not working i have tried many method my gps location is on in phone this code works on emulator but not working in device i have tried onLocationChanged method also please any buddy suggest me a proper working solution
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, mLocationListener);
Criteria mCriteria = new Criteria();
String bestProvider = String.valueOf(mLocationManager.getBestProvider(mCriteria, true));
Location mLocation = mLocationManager.getLastKnownLocation(bestProvider);
if (mLocation != null) {
Log.e("TAG", "GPS is on");
final double currentLatitude = mLocation.getLatitude();
final double currentLongitude = mLocation.getLongitude();
LatLng loc1 = new LatLng(currentLatitude, currentLongitude);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(currentLatitude, currentLongitude), 10));
mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 1000, null);
推荐答案
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude()))
.zoom(10)
.bearing(0.0f)
.tilt(00.0f)
.build();
map.moveCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
这篇关于Google Map上的当前位置更新编码无法在Android中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文