Sunday, July 7, 2013

Android Draw PolyLine in Google Map Version-2 | Google Map version-2 Integration in Android Draw polyLine

Hello Friends!
Today I am going to share very simple, useful, popular post on Google map version-2, for
drawing poly-line 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.manish.googlemap.R;
import com.google.android.gms.maps.CameraUpdateFactory;
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.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;
/**
 * @author manish
 *
 */
public class MainActivity extends FragmentActivity implements
  OnMapClickListener, OnMapLongClickListener{

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

 @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();
        googleMap.setMyLocationEnabled(true);
  
  googleMap.setOnMapClickListener(this);
  googleMap.setOnMapLongClickListener(this);
 
 }

 @Override
 public void onMapClick(LatLng point) {
  //add marker
  MarkerOptions marker=new MarkerOptions();
  marker.position(point);
  googleMap.addMarker(marker);
  // settin polyline in the map
    polylineOptions = new PolylineOptions();
    polylineOptions.color(Color.RED);
    polylineOptions.width(5);
    arrayPoints.add(point);
    polylineOptions.addAll(arrayPoints);
    googleMap.addPolyline(polylineOptions);
  }

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

}

(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!

15 comments:

  1. hi Sir , This ramesh ur rrequirement is how to draw a line from one country to another country in mapping version .. our code is working with in the country .. am getting error .. out of memory (enhanced) in android

    ReplyDelete
    Replies
    1. I think you select null lat lon.. plz test with hard code value..

      Delete
  2. please send the ramesh.puvvadi@gmail.com .. i hope will send the code

    ReplyDelete
  3. Hi Manish,
    I tried all the steps which you have said.but I cannot able to view it in the map.
    I needed to draw trails(lines) from current location to where-ever the user moves it should draw the line(we don't know the exact destination).
    I have searched for many snippets but could not find any solution.
    Please do help me in find a solution.If any sample code for that.

    Thanks,
    Naufal

    ReplyDelete
    Replies
    1. Dear it should work please check some important step.
      1-package name should be same in your project name at google developer console.
      2-check your manifest all permision.
      3-gps or network should be available.

      And as you can see this code work on touch so just use list for add ur point using location listner and update your map...

      Delete
  4. I have managed to get current position on the map but i am still not able to figure out how do i draw a poly line as i move, to track my movement. Please help if u could.

    ReplyDelete
    Replies
    1. use LocaationManager class to get current position and save that position in ArrayList and convert it into and display that path on google map.

      Delete
  5. Hi sir ,i need source code to lock my device using password and if password wrong then it must capture image from front camera and store in sd card. plz help me..

    ReplyDelete
  6. Hello sir,

    I tried to make the same code, I have no error, but norhing is drawed.

    I tried to put some log when I click on the map, but nothing happend, even the log. seems that onMapClick doesn't action.

    Help please.

    Regards.

    ReplyDelete
    Replies
    1. Did you create google map key successfully?
      android:name="com.google.android.maps.v2.API_KEY"

      Delete
    2. My bad, it works, thank you !

      Delete
  7. How would you make a polyline follow your location change?

    ReplyDelete
  8. sir how to store polyline in array polyline? I want to make new 5 separated polyline in maps, how to make it?

    ReplyDelete
    Replies
    1. You need to create 5 arraylist of latlng type and save lat lng value into it.

      Delete
    2. i solve my problem. thanks. but now, I want to save my polyline in mysql database, how to save it?? I browsing in internet, but there is no answer, it's very dificult. I have no idea sir

      Delete