Home » Android » google maps – how to zoom and add marker to android mapfragment

google maps – how to zoom and add marker to android mapfragment

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am displaying a map at my android application (google maps api v2).
I want to manipulate the map in order to show a specific location, zoom and marker.

can anyone give an example of manipulation mapfragment

How to&Answers:

To get map instance in code do this:

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

or:

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

depending on whether you used SupportMapFragment or MapFragment in your XML file.

Then to add a marker to it:

Marker newmarker = map.addMarker(new MarkerOptions().position(latlng).title("marker title").icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_purpul)));

To zoom the map:

CameraPosition cameraPosition = new CameraPosition.Builder().target(latlng).zoom(14.0f).build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
map.moveCamera(cameraUpdate);