Android Google Maps

android google maps

Android Google Maps

Android Google Maps – Android we can assimilate android google maps inside our application. Any location can be revealed by you on the map , or can show different routes on the map e.t.c. You could personalize the map relating to your alternatives also.

Android Google Maps – Design file/Layout file

You now have to include the map fragment into xml design file or layout file. Its syntax is listed below

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

 

Android Google Maps – AndroidManifest file

The next matter you must do is to include some permissions combined with the android Google Maps API type in the AndroidManifest.XML record. Its syntax is listed 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 Android Google Maps

You can certainly customize android google maps from its default view , and change it out corresponding to your demand.

Adding Marker

A maker can be located by you with some text over it displaying your location on the map. It could be done by via addMarker() method. Its syntax is listed below

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

 

Channing Map Type

You can also change the sort of the android google maps. You will discover four different kinds of android google maps and each give different view of the map. These kinds are Normal,Hybrid,Terrain and satellite. They can be utilized by you 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 even permit or disable the move gestures in the map by phoning the setZoomControlsEnabled(boolean) method. Its syntax is listed below

googleMap.getUiSettings().setZoomGesturesEnabled(true);

 

From these customization apart, there are other methods available in the Google Maps class , that can help you more customize the google maps. They can be listed below

Methods

  • addCircle(CircleOptions options)
  • addPolygon(PolygonOptions options)
  • addTileOverlay(TileOverlayOptions options)
  • animateCamera(CameraUpdate update)
  • clear()
  • getMyLocation()
  • moveCamera(CameraUpdate update)
  • setTrafficEnabled(boolean enabled)
  • snapshot(GoogleMap.SnapshotReadyCallback callback)
  • stopAnimation()

Android Google Maps Example

Here is a good example of android google maps demonstrating the utilization of android Google Maps class. It creates a simple M application which allows you to understand through the map.To test out this example android google maps, you can run this on a genuine device or within an emulator.

Create a task with android google maps activity as shown below

android google maps

It will start the following display screen and replicate the console web address for API Key as shown below

android google maps

Backup this and paste it to your web browser. It shall supply the pursuing display

android google maps

Select continue and select Create API Key it’ll show the next display screen then

android google maps

This is actually the content of MapActivity.java.

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:map="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/map"
   android:name="com.google.android.gms.maps.SupportMapFragment"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="com.example.androdevelopment.myapplication.MapsActivity" />

Within the below code we’ve given test latitude and longitude details

package com.example.androdevelopment.myapplication;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

   private GoogleMap mMap;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_maps);
      // Obtain the SupportMapFragment and get notified when the map is ready to be used.
      SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
         .findFragmentById(R.id.map);
      mapFragment.getMapAsync(this);
   }
   
   /**
      * Manipulates the map once available.
      * This callback is triggered when the map is ready to be used.
      * This is where we can add markers or lines, add listeners or move the camera.
      * In this case, we just add a marker near Sydney, Australia.
      * If Google Play services is not installed on the device. 
      * This method will only be triggered once the user has installed 
         Google Play services and returned to the app.
   */
  
   @Override
   public void onMapReady(GoogleMap googleMap) {
      mMap = googleMap;
      // Add a marker in Sydney and move the camera
      LatLng AndroDevelopment = new LatLng(21, 57);
      mMap.addMarker(new 
         MarkerOptions().position(AndroDevelopment).title("androdevelopment.com"));
      mMap.moveCamera(CameraUpdateFactory.newLatLng(AndroDevelopment));
   }
}

 

Following is the content of AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androdevelopment.myapplication">

   <!--
      The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
      Google Maps Android API v2, but you must specify either coarse or fine
      location permissions for the 'MyLocation' functionality. 
   -->
    
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
   <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
   <uses-permission android:name="android.permission.INTERNET" />
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">

      <!--
         The API key for Google Maps-based APIs is defined as a string resource.
         (See the file "res/values/google_maps_api.xml").
         Note that the API key is linked to the encryption key used to sign the APK.
         You need a different API key for each encryption key, including the release key 
         that is used to sign the APK for publishing.
         You can define the keys for the debug and 
            release targets in src/debug/ and src/release/. 
      -->
      
      <meta-data
         android:name="com.google.android.geo.API_KEY"
         android:value="AIzaSyAXhBdyKxUo_cb-EkSgWJQTdqR0QjLcqes" />

      <activity
         android:name=".MapsActivity"
         android:label="@string/title_activity_maps">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>

</manifest>

 

Output should be like this

android google maps