Friday, July 5, 2013

Android Draw Polygon in Google Map Version-2 | Custom Marker in Google Map Version-2 | Google Map version-2 Integration in Android Draw polygon

Hello Friends!
Today I am going to share very simple, useful, popular post on Google map version-2, for
drawing polygon and custom marker on tap on Google map.
Important steps are given below-
1)Generate SHA1 certificate for your Google map from google developer account from below link-
https://code.google.com/apis/
2)Create new android project.
3)Copy paste below given activity and layout.
4)Add Internet permission in your manifest.
5)Put your map key in manifest.
7)Enable your Network connection or GPS.
8)Run your project and tap on map for draw circle and marker.
9)Long press for remove circle.
10)Put your comment for more help.

(A)Print Screen:


(B)MainActivity:


package com.manish.googlemap;

import java.util.ArrayList;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

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.GoogleMap.OnMarkerClickListener;
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.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polygon;
import com.google.android.gms.maps.model.PolygonOptions;
import com.google.android.gms.maps.model.PolylineOptions;

/**
 * @author manish
 * 
 */
public class MainActivity extends FragmentActivity implements
  OnMapClickListener, OnMapLongClickListener, OnMarkerClickListener {

 private GoogleMap googleMap;
 private ArrayList<LatLng> arrayPoints = null;
 PolylineOptions polylineOptions;
 private boolean checkClick = false;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  arrayPoints = new ArrayList<LatLng>();
  SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
    .findFragmentById(R.id.map);
  googleMap = fm.getMap();
  // display zoom map
  googleMap.setMyLocationEnabled(true);

  googleMap.setOnMapClickListener(this);
  googleMap.setOnMapLongClickListener(this);
  googleMap.setOnMarkerClickListener(this);

 }

 @Override
 public void onMapClick(LatLng point) {
  if (checkClick == false) {

   googleMap.addMarker(new MarkerOptions().position(point).icon(
     BitmapDescriptorFactory.fromResource(R.drawable.point)));
   arrayPoints.add(point);
  }
 }

 @Override
 public void onMapLongClick(LatLng point) {
  googleMap.clear();
  arrayPoints.clear();
  checkClick = false;
 }

 public void countPolygonPoints() {
  if (arrayPoints.size() >= 3) {
   checkClick = true;
   PolygonOptions polygonOptions = new PolygonOptions();
   polygonOptions.addAll(arrayPoints);
   polygonOptions.strokeColor(Color.BLUE);
   polygonOptions.strokeWidth(7);
   polygonOptions.fillColor(Color.CYAN);
   Polygon polygon = googleMap.addPolygon(polygonOptions);
  }
 }

 @Override
 public boolean onMarkerClick(Marker marker) {
  // TODO Auto-generated method stub
  System.out.println("Marker lat long=" + marker.getPosition());
  System.out.println("First postion check" + arrayPoints.get(0));
  System.out
    .println("**********All arrayPoints***********" + arrayPoints);
  if (arrayPoints.get(0).equals(marker.getPosition())) {
   System.out.println("********First Point choose************");
   countPolygonPoints();
  }
  return false;
 }
}

(C)activity_main.xml:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        class="com.google.android.gms.maps.SupportMapFragment" />
     
</RelativeLayout>

(D)AndroidManifest.xml:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.manish.googlemap"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.manish.googlemap.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="Put Your Map Key Here!" />    </application>

</manifest>


Thanks!
 Please put your comment and suggestion for more help!

6 comments:

  1. Good Evening Manish,

    I am daily visit your site and it contains good and usefull content.so i have implemented facebook login in my android application.but i don know how to create logout button in android.so please provide me the sample code.try to help me.
    Thanks in advance.
    bye.

    ReplyDelete
  2. Manish - great series of tutorials. It is very useful for a android newbie like me. I think, more than being just general purpose tutorials, I like the fact that you have taken a series of thoughtful (and seemingly difficult) topics and made it accessible to us. Kudos to you.

    I was following along a tutorial and I wanted to know what if any would be the steps necesary to extend this polygon approach to querying for a bunch of things, e.g., within the polygon on the map, I want to query for all the Starbucks locations. It would be personally very useful for me and I think may benefit your community of followers as well.

    Thank you for your time,
    Siva

    ReplyDelete
  3. GoogleMap cannot resolve it into type.
    can u tell me how and what to import to the project

    ReplyDelete
  4. Can you please tell me, How to get Altitude in android?

    ReplyDelete