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="17" /> <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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <fragment android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.SupportMapFragment"/>
Step 5 :- Open MainActivity.java
package com.mapv2.demo; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.view.Menu; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.GoogleMap.OnMapClickListener; import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.BitmapDescriptorFactory; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; public class MainActivity extends FragmentActivity { GoogleMap map; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); // Getting reference to Google Map map = fm.getMap(); // OnClick Event listener for the Google Map map.setOnMapClickListener(new OnMapClickListener() { @Override public void onMapClick(LatLng point) { // Creating an instance of MarkerOptions MarkerOptions markerOptions = new MarkerOptions(); // Setting position for the marker markerOptions.position(point); // Setting custom icon for the marker markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)); // Setting title for the infowindow markerOptions.title(point.latitude + ","+point.longitude); // Adding the marker to the map map.addMarker(markerOptions); } }); // LongClick Event listener for the Google Map map.setOnMapLongClickListener(new OnMapLongClickListener() { @Override public void onMapLongClick(LatLng point) { // Clear all the markers from the Google Map map.clear(); } }); } @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; } }
Step 6 :- Run Code.
Leave a Reply