Android Google Maps API V2 缩放至当前位置 [英] Android Google Maps API V2 Zoom to Current Location

查看:29
本文介绍了Android Google Maps API V2 缩放至当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Maps API V2 以更加熟悉它,并且我正在尝试以用户当前位置为中心启动地图.使用 map.setMyLocationEnabled(true); 语句,我可以在地图上显示我的当前位置.这还会向 UI 中添加按钮,使地图以我的当前位置为中心.

I'm trying to mess around with the Maps API V2 to get more familiar with it, and I'm trying to start the map centered at the user's current location. Using the map.setMyLocationEnabled(true); statement, I am able to show my current location on the map. This also adds the button to the UI that centers the map on my current location.

我想在我的代码中模拟那个按钮按下.我熟悉 LocationManagerLocationListener 类并意识到使用它们是一种可行的替代方法,但似乎已经构建了将用户位置居中和放大的功能通过按钮进入.

I want to simulate that button press in my code. I am familiar with the LocationManager and LocationListener classes and realize that using those is a viable alternative, but the functionality to center and zoom in on the user's location seems to already be built in through the button.

如果 API 有显示用户当前位置的方法,那么肯定有比使用 LocationManager/LocationListener 类更简单的方法来以位置为中心,对吗?

If the API has a method to show the user's current location, there surely must be an easier way to center on the location than to use the LocationManager/LocationListener classes, right?

推荐答案

试试这个编码:

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();

Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
if (location != null)
{
    map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 13));

    CameraPosition cameraPosition = new CameraPosition.Builder()
        .target(new LatLng(location.getLatitude(), location.getLongitude()))      // Sets the center of the map to location user
        .zoom(17)                   // Sets the zoom
        .bearing(90)                // Sets the orientation of the camera to east
        .tilt(40)                   // Sets the tilt of the camera to 30 degrees
        .build();                   // Creates a CameraPosition from the builder
    map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));       
}

这篇关于Android Google Maps API V2 缩放至当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆