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.
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.
Thanks!
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!
setContentView(R.layout.activity_main);
ReplyDeleteSupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
for me the above lines error coming. at this places activity_main and R.id.map
activity_main cannot be resolved or is not a field
ReplyDeletethis 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.
Problem with your manifest file see this line..
DeleteYou 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,
bro.. problem not solved. same error occurred... help me please...
ReplyDeletePlease see on my post a download link here also..
Deletei changed that thing u told above... but stil there same error...
ReplyDeleteI have added zip code on GIT Server please download it and test it-
Deletehttps://github.com/manishsri01/GoogleMapCircleDemo
how can find the current Longitude,latitude
ReplyDeletePlease use Location manager for getting current lat and lon. you can check my this post-
Deletehttp://www.androidhub4you.com/2012/09/location-manager-demo-in-android.html
Hi, How to draw dashed/ dotted circle in map v2 Android?
ReplyDeletenice 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
ReplyDeleteI 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).
Deletewell 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
no error but when i run in my phone.."Unfortuntly,google map stopped."why??
ReplyDeletePlease check log-cat for reason for stopped google map and post here so i can help you..
DeleteThanks!
nothing display on log-cat
Deleteerror at Binary file line #8.Error at inflating class fragment.
DeleteAndroidManifest.xml does not have right value...so how....the version of API and android is related??
I go what issue you are getting.. You need one more permission now in your manifest for updated Google Play Services Library.
DeleteAnd still you have issue then please check google doc they have added many new permission like
@Integer etc..
if getSupportFragmentManager() is undefine .......so you can use
ReplyDeleteMainActivity extends FragmentActivity
Yes we are doing same..
Deletehello Manish
DeleteCan 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
@Sambit, android SDK does not guarantee to get phone number using Telephony Manager.
DeleteThanxx for reply...If you get any code...then please share it with me..Thank you so much...
DeleteHow to use Mapv2 with tabbar where 2 different tab shown map.....please help
ReplyDelete