Activity_main :
?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.swapnil.mapclg.MainActivity"> <!-- change to proper context-->
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="51dp"
android:text="Taj Mahal"
android:layout_marginLeft="132dp"
android:layout_marginStart="132dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="Eiffel Tower"
android:layout_below="@+id/b1"
android:layout_alignLeft="@+id/b1"
android:layout_alignStart="@+id/b1" />
<Button
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="Greate Wall of china"
android:layout_below="@+id/b2"
android:layout_alignLeft="@+id/b2"
android:layout_alignStart="@+id/b2" />
</RelativeLayout>
Activity_maps.xml :
<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.swapnil.mapclg.MapsActivity" /> <!-- change to proper context-->
MainActivity.java :
package com.example.swapnil.mapclg;// keep your package as it is and copy paste from next line
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button b1,b2,b3;
Intent ii;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.b1);
b2=(Button)findViewById(R.id.b2);
b3=(Button)findViewById(R.id.b3);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ii=new Intent(getApplicationContext(),MapsActivity.class);
ii.putExtra("lati","27.1750");
ii.putExtra("longi","78.0422");
ii.putExtra("desc","Symbol of love");
startActivity(ii);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { ii=new Intent(getApplicationContext(),MapsActivity.class);
ii.putExtra("lati","48.858093");
ii.putExtra("longi","2.294694");
ii.putExtra("desc","currently the most famous symbol of Paris.");
startActivity(ii);
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ii=new Intent(getApplicationContext(),MapsActivity.class);
ii.putExtra("lati","40.431908");
ii.putExtra("longi","116.570374");
ii.putExtra("desc","represents the unification of China");
startActivity(ii);
}
});
}
}
MapsActivity.java :
package com.example.swapnil.mapclg; // keep your package as it is and copy paste from next line
import android.support.annotation.FloatRange;
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);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
float lat= Float.parseFloat(getIntent().getStringExtra("lati"));
float longi=Float.parseFloat(getIntent().getStringExtra("longi"));
String a=getIntent().getStringExtra("desc");
LatLng sydney = new LatLng(lat,longi);
mMap.addMarker(new MarkerOptions().position(sydney).title(a));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
AndroidManifest.xml :
Write below line exactly above <application> tag
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.swapnil.mapclg.MainActivity"> <!-- change to proper context-->
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="51dp"
android:text="Taj Mahal"
android:layout_marginLeft="132dp"
android:layout_marginStart="132dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="Eiffel Tower"
android:layout_below="@+id/b1"
android:layout_alignLeft="@+id/b1"
android:layout_alignStart="@+id/b1" />
<Button
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="Greate Wall of china"
android:layout_below="@+id/b2"
android:layout_alignLeft="@+id/b2"
android:layout_alignStart="@+id/b2" />
</RelativeLayout>
Activity_maps.xml :
<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.swapnil.mapclg.MapsActivity" /> <!-- change to proper context-->
MainActivity.java :
package com.example.swapnil.mapclg;// keep your package as it is and copy paste from next line
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button b1,b2,b3;
Intent ii;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.b1);
b2=(Button)findViewById(R.id.b2);
b3=(Button)findViewById(R.id.b3);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ii=new Intent(getApplicationContext(),MapsActivity.class);
ii.putExtra("lati","27.1750");
ii.putExtra("longi","78.0422");
ii.putExtra("desc","Symbol of love");
startActivity(ii);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { ii=new Intent(getApplicationContext(),MapsActivity.class);
ii.putExtra("lati","48.858093");
ii.putExtra("longi","2.294694");
ii.putExtra("desc","currently the most famous symbol of Paris.");
startActivity(ii);
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ii=new Intent(getApplicationContext(),MapsActivity.class);
ii.putExtra("lati","40.431908");
ii.putExtra("longi","116.570374");
ii.putExtra("desc","represents the unification of China");
startActivity(ii);
}
});
}
}
MapsActivity.java :
package com.example.swapnil.mapclg; // keep your package as it is and copy paste from next line
import android.support.annotation.FloatRange;
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);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
float lat= Float.parseFloat(getIntent().getStringExtra("lati"));
float longi=Float.parseFloat(getIntent().getStringExtra("longi"));
String a=getIntent().getStringExtra("desc");
LatLng sydney = new LatLng(lat,longi);
mMap.addMarker(new MarkerOptions().position(sydney).title(a));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
AndroidManifest.xml :
Write below line exactly above <application> tag
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Output :