The Open Event Android project helps event organizers to generate Apps (apk format) for their events/conferences by providing API endpoint or zip generated using Open Event server. In the Open Event Android App, we have a map for showing all locations of sessions. In this map users should be able to see different locations with multiple markers. In this post I explain how to add multiple markers in the Google map fragment and set the bound in the map so that all markers are visible on one screen with specified padding.
Create Map Fragment
The first step to do is to create a simple xml file and add a fragment element in it. Then create MapsFragment.java file and find fragment element added in the xml file using findFragmentById() method.
SupportMapFragment supportMapFragment = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map));
1. Create fragment_map.xml file
In this file add FrameLayout as a top level element and add fragment element inside FrameLayout with the name “com.google.android.gms.maps.SupportMapFragment”.
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <fragment android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout>
2. Create MapsFragment.java
MapsFragment.java extends SupportMapFragement and implements LocationListener, OnMapReadyCallback. Make instance of GoogleMap object. In onViewCreated method inflate fragment_map.xml file using inflater. Now find the fragment element added in the xml file using findFragmentById() method and assign it to SupportMapFragement instance.
public class MapsFragment extends SupportMapFragment implements LocationListener, OnMapReadyCallback { private GoogleMap mMap; @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_map, container, false); SupportMapFragment supportMapFragment = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)); supportMapFragment.getMapAsync(this); return view; } @Override public void onMapReady(GoogleMap map) { … } ... }
Create list of locations
Location object has three variables name, latitude & longitude. Create a list of locations for which we want to add markers to the map.
public class Location { private String name; private float latitude; private float longitude; } List<Microlocation> mLocations= new ArrayList<>();
Add location objects in mLocations in onViewCreated() method
mLocations.add(location);
You can add multiple locations using for loop or fetching from the database.
Add markers
Add following code in onMapReady(GoogleMap map) method. onMapReady(GoogleMap map) is called when map is ready to be used. setMapToolbarEnabled(true) used to show toolbar for marker. If the toolbar is enabled users will see a bar with various context-dependent actions, including ‘open this map in the Google Maps app’ and ‘find directions to the highlighted marker in the Google Maps app’.
if(map != null){ mMap = map; mMap.getUiSettings().setMapToolbarEnabled(true); } showEventLocationOnMap();
Create showEventLocationOnMap() method and add following code
private void showLocationsOnMap(){ float latitude; float longitude; Marker marker; //Add markers for all locations for (Location location : mLocations) { latitude = location.getLatitude(); longitude = location.getLongitude(); latlang = new LatLng(latitude, longitude); marker = mMap.addMarker(new MarkerOptions() .position(latlang) .title(location.getName())); } }
So what we are doing here?
For each location object in mLocations list, we are creating LatLng(latitude, longitude) object and adding it to the map using
marker = mMap.addMarker(new MarkerOptions().position(latlang).title(location.getName()));
Setting bound
We want to set the zoom level so that all markers are visible. To do this, we can use LatLngBounds.Builder. Add the position of the marker in the builder object using include method while adding the marker in the map.
LatLngBounds.Builder builder = new LatLngBounds.Builder(); builder.include(marker.getPosition());
Then make the LatLngBounds object from builder.
LatLngBounds bounds = builder.build(); CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, dpToPx(40)); mMap.moveCamera(cameraUpdate); private int dpToPx(int dp) { return (int) (dp * Resources.getSystem().getDisplayMetrics().density); }
dpTopx converts dp to px so that it set the same padding in all devices.
Conclusion
Markers in map give very nice user experience for an event or a conference because they show the relative position of places and we now offer this feature in Open Event Android.
Additional resources:
- How to add Map to an Android App : Google maps documentation
- To add styled map in App follow this link
You must log in to post a comment.