Step 1 :- Create New Android Project.
Step 2 :- Add Google-play-services_lib to your project.
Step 3 :- Open AndroidManifest.xml file.
<!--?xml version="1.0" encoding="utf-8"?--> package="com.mapv2.demo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <permission android:name="com.mapv2.demo.permission.MAPS_RECEIVE" android:protectionLevel="signature"/> <uses-feature android:glEsVersion="0x00020000" android:required="true"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.mapv2.demo.MainActivity" android:label="@string/app_name" > <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyCmX7SLVHXxU9pSqb2QbAOvdnjAGUulOrk"/>
Step 4 :- Open activity_main.xml.
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.SupportMapFragment"/>
1.) info_window_layout.xml
<!--?xml version="1.0" encoding="utf-8"?--> android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:background="@color/bg_color" > <TextView android:id="@+id/tv_lat" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_lng" android:layout_width="wrap_content" android:layout_height="wrap_content" />
Step 5 :- Open MainActivity.java
package com.mapv2.demo; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.view.Menu; import android.view.View; import android.widget.TextView; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter; import com.google.android.gms.maps.GoogleMap.OnMapClickListener; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; public class MainActivity extends FragmentActivity { GoogleMap googleMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Getting reference to the SupportMapFragment of activity_main.xml SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); // Getting GoogleMap object from the fragment googleMap = mapFragment.getMap(); // Setting a custom info window adapter for the google map googleMap.setInfoWindowAdapter(new InfoWindowAdapter() { // Use default InfoWindow frame @Override public View getInfoWindow(Marker arg0) { return null; } // Defines the contents of the InfoWindow @Override public View getInfoContents(Marker arg0) { // Getting view from the layout file info_window_layout View v = getLayoutInflater().inflate(R.layout.info_window_layout, null); // Getting the position from the marker LatLng latLng = arg0.getPosition(); // Getting reference to the TextView to set latitude TextView tvLat = (TextView) v.findViewById(R.id.tv_lat); // Getting reference to the TextView to set longitude TextView tvLng = (TextView) v.findViewById(R.id.tv_lng); // Setting the latitude tvLat.setText("Latitude:" + latLng.latitude); // Setting the longitude tvLng.setText("Longitude:"+ latLng.longitude); // Returning the view containing InfoWindow contents return v; } }); // Adding and showing marker while touching the GoogleMap googleMap.setOnMapClickListener(new OnMapClickListener() { @Override public void onMapClick(LatLng arg0) { // Clears any existing markers from the GoogleMap googleMap.clear(); // Creating an instance of MarkerOptions to set position MarkerOptions markerOptions = new MarkerOptions(); // Setting position on the MarkerOptions markerOptions.position(arg0); // Animating to the currently touched position googleMap.animateCamera(CameraUpdateFactory.newLatLng(arg0)); // Adding marker on the GoogleMap Marker marker = googleMap.addMarker(markerOptions); // Showing InfoWindow on the GoogleMap marker.showInfoWindow(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
Step 6 :- Run Code.
Leave a Reply