Like Us on Facebook

How to Use GoogleMap API v3 in Android

Hi, Today I am going to explain how to use Google Map API v3 in Android Application. Before going to start  this tutorial we  will go through some key elements of Google Map. This steps  are necessary to configure a development environment to work with Google Maps and then work through some code examples demonstrating some of the basics of Google Maps Android integration.
 
GoogleMap – It is the main class of the Google Maps Android API. This class is responsible for downloading and displaying map tiles and for displaying and responding to map controls.
 
MapView - A subclass of the View class, provides the view canvas onto which the map is drawn by the Google Map object.
 
SupportMapFragment – A subclass of the Fragment class, allows a map to be placed within a Fragment in an Android layout.
 
Marker – The purpose of the Marker class is to allow locations to be marked on a Map.

 
Prerequisites:
 
  • JDK 7.0 or above
  • Android Studio 2.0


Steps to follow:  
 
  1. Create a New Android Studio project name MapDemo.
  2. Select minimum API level 19 (KitKat) to Support Google Maps API v3 to  Support  Maximum  of Android device available in Google play.
  3. Select Google MapsActivity and your Activity name is MapsActivity click next.
  4. Give your layout xml name is activity_map_demo And click finish
 
      5. Now to Obtain Developer signature  go to google_maps_api.xml from app -> res -> values folder of the project . google_maps_api.xml file is a link to the Google   Developer console.Copy and paste this link into a browser window. Once loaded, a page similar  to the following will appear:
 
 
 
 
 
 
      6. Verify that the menu is set to Create a new project before clicking on the Continue button.Once the API has been enabled, click on the Go to Credentials button.
After create a new Project  a panel will appear providing the option to create an Android key for the application like below.
 

 
 

      7. Check that the SHA-1 fingerprint and application package name match those listed in the google_maps_api.xml file and click on the Create button to generate the API key for your application. A dialog window will subsequently appear containing the API key for the app. Copy this key, return to Android Studio and paste the API key into the
YOUR_KEY_HERE section of the file:
 
<string name=“google_maps_key” templateMergeStrategy=“preserve”>
YOUR_KEY_HERE
</string>
 
 

         8. Now within MapsAcivity.java write the following code .
 
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
 
 
    private GoogleMap mMap;
    private static final int LOCATION_REQUEST_CODE = 101;
    private String TAG = "MapDemo";
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_demo);
 
        requestPermission(Manifest.permission.ACCESS_FINE_LOCATION,
                LOCATION_REQUEST_CODE);
        // 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);
    }
 
 
    protected void requestPermission(String permissionType, int
            requestCode) {
        int permission = ContextCompat.checkSelfPermission(this,
                permissionType);
        if (permission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{permissionType}, requestCode
            );
        }
    }
 
 
    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[]
            grantResults) {
        switch (requestCode) {
            case LOCATION_REQUEST_CODE: {
                if (grantResults.length == 0
                        || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "Unable to show location - permission required", Toast.LENGTH_LONG).show();
                }
                return;
            }
        }
    }
 
 
    /**
     * 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, the user will be prompted to install
     * it inside the SupportMapFragment. 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 sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
 
    }
}
 



         9. And Configure your Manifest file is like below.
 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tutorial.mapdemo">
 
    <!--
         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.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 
 
    <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="YOUR_KEY_HERE" />
 
        <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>

Posted By UandBlog

UandBlog is a Global Leading source of Finance, Health, Lifestyle, Technology, Gaming and Programing and other information, it is a fastest growing blog for all , for latest post or information like us on facebook , follow on Twitter, Google+ and Pinterest.

Comments ( 0 )

    Leave a comment..

    Click to login.

    Subscribe Latest Information

    Most Popular Posts

    You May Like Also