fastread homefastrread library fastread menu

Android : Google Maps

Tutorial by:      Date: 2016-05-12 04:35:07

❰ Previous Next ❱

Android allows us to integrate google maps in our application. You can show any location on the map , or can show different routes on the map e.t.c. You can also customize the map according to your choices.

Adding Google Map

Google provides this facility using google play services library which you have to download externally. After downloading, you have to integrate it with your project.In the end you have to integrate your application with google via google console. This is completely discussed in the example.

Google Map - Activity file

Google provides GoogleMap and MapFragment api to integrate map in your android application. In order to use GoogleMap , you have to create an object of GoogleMap and get the reference of map from the xml layout file.Its syntax is given below −

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

Google Map - Layout file

Now you have to add the map fragment into xml layout file. Its syntax is given below −

<fragment
   android:id="@+id/map"
   android:name="com.google.android.gms.maps.MapFragment"
   android:layout_width="match_parent"
   android:layout_height="match_parent"/>

Google Map - AndroidManifest file

The next thing you need to do is to add some permissions along with the Google Map API key in the AndroidManifest.XML file. Its syntax is given below −

<!--Permissions-->

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!--Google MAP API key-->

<meta-data
   android:name="com.google.android.maps.v2.API_KEY"
   android:value="AIzaSyDKymeBXNeiFWY5jRUejv6zItpmr2MVyQ0" />        

Customizing Google Map

You can easily customize google map from its default view , and change it according to your demand.

Adding Marker

You can place a maker with some text over it displaying your location on the map. It can be done by via addMarker() method. Its syntax is given below −

final LatLng TutorialsPoint = new LatLng(21 , 57);
Marker TP = googleMap.addMarker(new MarkerOptions().position(TutorialsPoint).title("TutorialsPoint")); 

Channing Map Type

You can also change the type of the MAP. There are four different types of map and each give different view of the map. These types are Normal,Hybrid,Satellite and terrain. You can use them as below

googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

Enable/Disable zoom

You can also enable or disable the zoom gestures in the map by calling the setZoomControlsEnabled(boolean) method. Its syntax is given below −

googleMap.getUiSettings().setZoomGesturesEnabled(true);

Apart from these customization, there are other methods available in the GoogleMap class , that helps you more customize the map. They are listed below −

Sr.No Method & description
1 addCircle(CircleOptions options)

This method add a circle to the map

2 addPolygon(PolygonOptions options)

This method add a polygon to the map

3 addTileOverlay(TileOverlayOptions options)

This method add tile overlay to the map

4 animateCamera(CameraUpdate update)

This method Moves the map according to the update with an animation

5 clear()

This method removes everything from the map.

6 getMyLocation()

This method returns the currently displayed user location.

7 moveCamera(CameraUpdate update)

This method repositions the camera according to the instructions defined in the update

8 setTrafficEnabled(boolean enabled)

This method Toggles the traffic layer on or off.

9 snapshot(GoogleMap.SnapshotReadyCallback callback)

This method Takes a snapshot of the map

10 stopAnimation()

This method stops the camera animation if there is one in progress

 

❰ Previous Next ❱