Wednesday, July 3, 2013

Android Google Map Version-2 Integration | Draw Circle on Google Map Version-2 | Draw Marker on Google Map Version-2

Hello Friends!
Today I am going to share very simple, useful, popular post on Google map version-2, for
drawing circle and 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.java :


package com.manish.googlemap;

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;
/**
 * @author manish
 *
 */
public class MainActivity extends FragmentActivity implements
  OnMapClickListener, OnMapLongClickListener{

 private GoogleMap googleMap;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
  googleMap = fm.getMap();
  //display zoom map
     googleMap.moveCamera( CameraUpdateFactory.zoomTo(13.2f));
        // Enabling MyLocation Layer of Google Map
        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);
  //add circle
  CircleOptions circle=new CircleOptions();
  circle.center(point).fillColor(Color.LTGRAY).radius(1000);
  googleMap.addCircle(circle);
  }

 @Override
 public void onMapLongClick(LatLng point) {
  googleMap.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>

(E) DOWNLOAD ZIP CODE-


Thanks!

23 comments:

  1. setContentView(R.layout.activity_main);
    SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);


    for me the above lines error coming. at this places activity_main and R.id.map

    ReplyDelete
  2. activity_main cannot be resolved or is not a field

    this error came at first line of the above comment.

    map cannot be resolved or is not a field

    this error came at second line of the above comment.

    ReplyDelete
    Replies
    1. Problem with your manifest file see this line..
      You have more than one package in your appliaction?
      if no just try this-
      in your manifest-
      xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.manish.googlemap"

      Here package name should be same in your MainActivity.java file also-
      See at the top of your activity class-
      package com.manish.googlemap;

      Just make same all package name and press ctrl+shift+O and enjoy..

      Thanks,

      Delete
  3. bro.. problem not solved. same error occurred... help me please...

    ReplyDelete
  4. i changed that thing u told above... but stil there same error...

    ReplyDelete
    Replies
    1. I have added zip code on GIT Server please download it and test it-
      https://github.com/manishsri01/GoogleMapCircleDemo

      Delete
  5. how can find the current Longitude,latitude

    ReplyDelete
    Replies
    1. Please use Location manager for getting current lat and lon. you can check my this post-
      http://www.androidhub4you.com/2012/09/location-manager-demo-in-android.html

      Delete
  6. Hi, How to draw dashed/ dotted circle in map v2 Android?

    ReplyDelete
  7. nice tutorial......thnx....bt i want 2 know can we draw circle on Google map by dragging user finger just like canvas pencil.........plz give me some suitable suggestion nd solution if u can

    ReplyDelete
    Replies
    1. I think you can't it on google map. On google map circle need only (lat,lon) and radius, but in case of canvas you get a path with mid point(x,y).

      well you can do one thing hope it will help you-
      1)Create a framlayout on google-map with transparent background so user can see map.
      2)Create canvas on that framlayout and draw circle on drag and put a done button below of that circle .
      3)now calculate radius of this canvas circle.
      4)On done button click update your app with this circle and hide that framlayout.

      Thanks

      Delete
  8. no error but when i run in my phone.."Unfortuntly,google map stopped."why??

    ReplyDelete
    Replies
    1. Please check log-cat for reason for stopped google map and post here so i can help you..

      Thanks!

      Delete
    2. error at Binary file line #8.Error at inflating class fragment.
      AndroidManifest.xml does not have right value...so how....the version of API and android is related??

      Delete
    3. I go what issue you are getting.. You need one more permission now in your manifest for updated Google Play Services Library.


      And still you have issue then please check google doc they have added many new permission like
      @Integer etc..

      Delete
  9. if getSupportFragmentManager() is undefine .......so you can use

    MainActivity extends FragmentActivity

    ReplyDelete
    Replies
    1. hello Manish

      Can u give me any code which return user mobile number,in

      case1:single slot single sim smart phone(getLine1Number() not working)
      case2:dual slot single sim smartphone(getLine1Number() not working)
      case3:dual sim dual slot smartphone (getLine1Number() workin fine)


      solution to 1 and 2 is quite more reqired............thanxxxx in advance

      Delete
    2. @Sambit, android SDK does not guarantee to get phone number using Telephony Manager.

      Delete
    3. Thanxx for reply...If you get any code...then please share it with me..Thank you so much...

      Delete
  10. How to use Mapv2 with tabbar where 2 different tab shown map.....please help

    ReplyDelete