简体   繁体   中英

Java Android Google maps change zoom controls position and set camera to marker

I want the zoom controls to be on the very top in the middle, I tried to do something like this but it does not work:

  SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().
                findFragmentById(R.id.map);
        View mapView = mapFragment.getView();
View zoom_in_button = mapView.findViewWithTag("GoogleMapZoomInButton");


RelativeLayout.LayoutParams location_layout = (RelativeLayout.LayoutParams) zoom_in_button.getLayoutParams();
        location_layout.addRule(RelativeLayout.CENTER_HORIZONTAL, 0);

Also I need to get this: center the view so that the marker is in the bottom left corner

I did this but it does not work.

mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(currentPGO.getLatitude(), currentPGO.getLongitude()), 16));

as far as I know, you cant change the Map controls you can just hide them and make your own zoom in and out buttons.

map.getUiSettings().setZoomControlsEnabled(false);

and then make your buttons in a relative layout or constraints layout and position them as you wish.

and then to make them zoom in and out

(Snippet from google maps api for Android) https://developers.google.com/maps/documentation/android-sdk/views

CameraUpdateFactory.zoomIn() and CameraUpdateFactory.zoomOut()// give you a CameraUpdate that changes the zoom level by 1.0, while keeping all other properties the same.
  1. Hide default zoom buttons of Google Maps

    mMap.uiSettings.isZoomControlsEnabled = false

  2. Create ImageView/Button widget for Zoom In and Zoom Out (Align this on Top Center or wherever you prefer, above the fragment)

  3. Call the below methods on Widget click:

    Zoom In

    mMap.animateCamera(CameraUpdateFactory.zoomIn())

    Zoom Out

    mMap.animateCamera(CameraUpdateFactory.zoomOut())

Edit: If you wish to place the marker on Lower Left corner, you will need to calculate new LatLang value which would be slightly to the right and top of desired location and move the camera accordingly. For example if your zoom level is 16, then use something like this:

var lat = markerLatLng.latitude
var lng = markerLatLng.longitude
mMap.addMarker(MarkerOptions().position(LatLng(lat, lng)))
lat += 0.005
lng += 0.003
moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(lat, lng), 16f))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM