单击 Android 按钮时获取 Google 地图中的当前位置
Get Current Position in Google Map when Click on Android Button
我在我的设备中成功实现了地图。
但现在我想添加功能
当我点击按钮时,我想在地图中获取当前位置。
我该怎么做?
只需单击该按钮即可启动位置更新侦听器。当新位置到达时,将 mapObject 中的相机(在 OnMapReady() 方法中获取)移动到带有动画的那个位置。
您可以在按钮点击时添加侦听器。
Button btnMyLocation = (Button) findViewById(R.id.btnMyLocation);
CameraUpdate cameraUpdate = null;
btnMyLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Location loc = map.getMyLocation();
if (loc != null) {
LatLng latLng = new LatLng(loc.getLatitude(), loc
.getLongitude());
cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 17);
map.animateCamera(cameraUpdate);
}
}
});
对于 GPS:
您需要在清单文件中获得以下权限
android.permission.ACCESS_FINE_LOCATION
这是代码:
final LocationManager manager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) )
Toast.makeText(context, "GPS is disable!", Toast.LENGTH_LONG).show();
else
Toast.makeText(context, "GPS is Enable!", Toast.LENGTH_LONG).show();
在OnClick方法中,通过LocationManager获取当前GPS位置(经纬度坐标)。有关操作方法的更多信息 here
我有类似的问题,在我的例子中是这样处理的:
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(loc.getLatitude(), loc.getLongitude()), 4));
此致
我在我的设备中成功实现了地图。
但现在我想添加功能
当我点击按钮时,我想在地图中获取当前位置。
我该怎么做?
只需单击该按钮即可启动位置更新侦听器。当新位置到达时,将 mapObject 中的相机(在 OnMapReady() 方法中获取)移动到带有动画的那个位置。
您可以在按钮点击时添加侦听器。
Button btnMyLocation = (Button) findViewById(R.id.btnMyLocation);
CameraUpdate cameraUpdate = null;
btnMyLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Location loc = map.getMyLocation();
if (loc != null) {
LatLng latLng = new LatLng(loc.getLatitude(), loc
.getLongitude());
cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 17);
map.animateCamera(cameraUpdate);
}
}
});
对于 GPS:
您需要在清单文件中获得以下权限
android.permission.ACCESS_FINE_LOCATION
这是代码:
final LocationManager manager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) )
Toast.makeText(context, "GPS is disable!", Toast.LENGTH_LONG).show();
else
Toast.makeText(context, "GPS is Enable!", Toast.LENGTH_LONG).show();
在OnClick方法中,通过LocationManager获取当前GPS位置(经纬度坐标)。有关操作方法的更多信息 here
我有类似的问题,在我的例子中是这样处理的:
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(loc.getLatitude(), loc.getLongitude()), 4));
此致