March 8, 2016

Google Map is one of the concept which is frequently used in our application. Since it is a vast topic, I’m going to explain all in a set of tutorials. In those tutorials there will be an explanation from simple Map loading to covering all the features of the Google Map. In this tutorial I’m going to show you how to load a basic MAP in your application.

STEP 1: Open your Android Studio, Create a new Project, and select GoogleMapActivity.
STEP 2: It will create three by default files, MapsActivity, the other is the activity_map.xml, and the last one is google_maps_api.xml.

activity_map.xml




google_maps_appi.xml




    
    paste your code here

MapsActivity

// This is the activity file where all the functionalities are to be done.

package com.mapstutorial;
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;
// This class will implement OnMapReadyCallback
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    // Take a reference of Google Map
    private GoogleMap googleMap;
    // provide the latitude and the longitude
    private double latitude = 12.850800;
    private double longitude = 77.676623;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // We will use here SupportMapFragment
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }
    @Override
    public void onMapReady(GoogleMap googleMap) {
        this.googleMap = googleMap;
        LatLng latLng = new LatLng(latitude, longitude);
        // MarkerOptions will point the icon to the given location(Latitude and Longitude)
        this.googleMap.addMarker(new MarkerOptions().position(latLng).title("Bangalore"));
        
        // Camera will move to the pointed location, we can play with the camera option.
        this.googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    }
}

EXPLANATION:

activity_map.xml.

It will contain one attribute as android:name=”com.google.android.gms.maps.SupportMapFragment”, this will automatically attach the Map in the activity, however through coding part also it can be done.

This will load a map with a marker pointing at Bangalore. I’ll explain you the code here.
1. The MapsActivity.java class will implements OnMapReadyCallBack
2. This interface has one method named onMapReady(GoogleMap googleMap).
3. This is the basic map loading. We can change here anything as per our requirement.
4. We will provide the latitude and the longitude.
5. Camera will point to the mentioned position, we will use MarkerOptions to point the latitude and the longitude.
6. We will use CameraUpdateFactory to view the location.
7. getMapAsync() it is used to set the callback of the fragment.

This will load a basic map in your application. However this is the basic simple loading of a map, its just a starting. From next tutorial we will explore more.