add dependency compile 'com.google.android.gms:play-services-maps:11.0.4'
add in manifest file
<permission
android:name="com.example.com.locationmap.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> and meta data
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCHrUMMMFTLannfAaqQ9RGVaTu301Ee0j8" />
code for xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
code for java class
public class MapActivity extends FragmentActivity implements
OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private GoogleMap mMap;
double latitude = 0;
double longitude = 0;
double latitude2 = 0;
double longitude2 = 0;
LatLng position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
latitude = getIntent().getDoubleExtra("lat", 0);
longitude = getIntent().getDoubleExtra("long", 0);
// Getting Reference to SupportMapFragment of activity_map.xml
SupportMapFragment fm = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
fm.getMapAsync(MapActivity.this);
}
@Override
public void onConnected(Bundle bundle) {
/* getDirection();*/
/*getCurrentLocation();*/
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public void onMapReady(GoogleMap googleMap) {
// Receiving latitude from MainActivity screen
latitude = getIntent().getDoubleExtra("lat", 0);
// Receiving longitude from MainActivity screen
longitude = getIntent().getDoubleExtra("long", 0);
latitude2 = 73.998;
// Receiving longitude from MainActivity screen
longitude2 = 22.4567;
position = new LatLng(latitude, longitude);
// Instantiating MarkerOptions class
MarkerOptions options = new MarkerOptions();
// Setting position for the MarkerOptions
options.position(position);
// Setting title for the MarkerOptions
options.title("Location");
// Setting snippet for the MarkerOptions
// Adding Marker on the Google Map
try {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude,
longitude, 1);
String cityName = addresses.get(0).getAddressLine(0);
String stateName = addresses.get(0).getAddressLine(1);
String countryName = addresses.get(0).getAddressLine(2);
options.snippet(cityName + "," + stateName);
} catch (Exception ex) {
ex.getMessage();
options.snippet("Latitude:" + latitude + ",Longitude:" + longitude);
}
googleMap.addMarker(options);
googleMap.addMarker(options.position(new LatLng(latitude2,
longitude2)));
// Creating CameraUpdate object for position
CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(position);
// Creating CameraUpdate object for zoom
CameraUpdate updateZoom = CameraUpdateFactory.zoomTo(6);
// Updating the camera position to the user input latitude and longitude
googleMap.moveCamera(updatePosition);
// Applying zoom to the marker position
googleMap.animateCamera(updateZoom);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
/*
getMenuInflater().inflate(R.menu.main, menu);
*/
return true;
}
}
`