Android的谷歌地图API V2放大到当前位置当前位置、大到、地图、Android

2023-09-12 07:34:17 作者:考试作弊、天经地义

我想惹周围的地图API V2,以获得更多的熟悉它,我想开始集中在用户的当前位置的地图。使用map.setMyLocationEnabled(真正的);声明中,我能够在地图上显示我的当前位置。这也增加了按钮的中心,对我目前的位置在地图的用户界面。

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.

我想模拟该按钮preSS在我的code。我熟悉的LocationManager和LocationListener的类和意识到,使用这些是一个可行的选择,但功能居中并放大用户的位置,似乎已通过按钮是内置的。

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 /监听器类的位置居中,对吧?

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/Listener classes...right?

任何帮助将是AP preciated!

Any help would be appreciated!

推荐答案

试试这个编码:

/////----------------------------------Zooming camera to position user-----------------

        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));

                    }

/////----------------------------------Zooming camera to position user-----------------