Android 让 Google 地图自动放大我的当前位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20316698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:37:56  来源:igfitidea点击:

Get Google Maps to automatically zoom in on my Current Location?

androidgoogle-maps

提问by Adz

Possible duplicate of Google Maps v2 - set both my location and zoom in

可能与Google Maps v2重复- 设置我的位置并放大

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_g_maps);
        GoogleMap googleMap;
        LatLng myPosition;


        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

        googleMap = fm.getMap();
        googleMap.setMyLocationEnabled(true);
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager.getLastKnownLocation(provider);

                if(location!=null){
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        LatLng latLng = new LatLng(latitude, longitude);
        myPosition = new LatLng(latitude, longitude);


    }

}
}

I've tried adding:

我试过添加:

CameraUpdate center=
        CameraUpdateFactory.newLatLng(new LatLng(latitude,
                                                 longitude));
    CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);

    googleMap.moveCamera(center);
    googleMap.animateCamera(zoom);

or

或者

 googleMap.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude) ,4) );

but Google Maps doesn't automatically zoom in once I've hit the button which calls this method. I still get the Google UI which I can click to zoom in on my Location, but I want it to automatically zoom in.

但是一旦我点击了调用这个方法的按钮,谷歌地图就不会自动放大。我仍然可以获得 Google UI,我可以点击它来放大我的位置,但我希望它自动放大。

Any help please?

请问有什么帮助吗?

回答by Shylendra Madda

Try this is simple solution for your question

试试这是您问题的简单解决方案

LatLng coordinate = new LatLng(lat, lng);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 5);
map.animateCamera(yourLocation);

and also..

并且..

It's possible to change location, zoom, bearing and tilt in one go. It is also possible to set the duration on the animatecamera call.

可以一次性更改位置、缩放、方位和倾斜。还可以设置 animatecamera 调用的持续时间。

CameraPosition cameraPosition = new CameraPosition.Builder()
    .target(MOUNTAIN_VIEW)      // Sets the center of the map to Mountain View
    .zoom(17)                   // Sets the zoom
    .bearing(90)                // Sets the orientation of the camera to east
    .tilt(30)                   // Sets the tilt of the camera to 30 degrees
    .build();                   // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

Have a look at the docs here:

看看这里的文档:

https://developers.google.com/maps/documentation/android/views?hl=fr-FR#moving_the_camera

https://developers.google.com/maps/documentation/android/views?hl=fr-FR#moving_the_camera

回答by sivaBE35

GoogleMap googleMap;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    googleMap = ((MapFragment) getFragmentManager().findFragmentById(
            R.id.map)).getMap();

    Location locationCt;
    LocationManager locationManagerCt = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationCt = locationManagerCt
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);

    LatLng latLng = new LatLng(locationCt.getLatitude(),
            locationCt.getLongitude());
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    googleMap.addMarker(new MarkerOptions().position(latLng)
            .title("My Spot").snippet("This is my spot!")
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_car)));

    googleMap.setMyLocationEnabled(true);

    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    // Zoom in the Google Map
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}

回答by Arpit Patel

In your XML file

在您的 XML 文件中

<Button
   android:id="@+id/Bzoomin"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:onClick="onZoom"
   android:text="^" />

   <Button
       android:id="@+id/Bzoomout"
       style="?android:attr/buttonStyleSmall"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_gravity="center_vertical"
       android:onClick="onZoom"
       android:text="v" />'

In your Java file

在您的 Java 文件中

 public void onZoom(View view)
 {
     if(view.getId() == R.id.Bzoomin)
     {
         mMap.animateCamera(CameraUpdateFactory.zoomIn());
     }
     if(view.getId() == R.id.Bzoomout)
     {
         mMap.animateCamera(CameraUpdateFactory.zoomOut());
     }
 }

回答by Gastón Saillén

For Kotlin

对于科特林

 val location = CameraUpdateFactory.newLatLngZoom(latlng, 15F)
 mMap.animateCamera(location)