Skip to main content

Android - Google MAP V2 PART 2



Before do this check part 1


Note : I have to run this in API 8 to API 17 that why i have used ActionBarSherlock support library. if you don't want that then replace it. 
In main Activity i have added following code.

 package com.djandroid.mapsv2;  
 import android.os.Bundle;  
 import android.support.v4.app.FragmentManager;  
 import com.actionbarsherlock.app.SherlockFragmentActivity;  
 import com.google.android.gms.maps.GoogleMap;  
 import com.google.android.gms.maps.GoogleMap.OnMapClickListener;  
 import com.google.android.gms.maps.SupportMapFragment;  
 import com.google.android.gms.maps.model.LatLng;  
 import com.google.android.gms.maps.model.MarkerOptions;  
 public class MainActivity extends SherlockFragmentActivity {  
      private GoogleMap MAP;  
      @Override  
      protected void onCreate(Bundle arg0) {  
           // TODO Auto-generated method stub  
           setTheme(R.style.Theme_Sherlock);  
           super.onCreate(arg0);  
           setContentView(R.layout.activity_main);  
           FragmentManager myFM = getSupportFragmentManager();  
           SupportMapFragment myMAPF = (SupportMapFragment) myFM  
                     .findFragmentById(R.id.fragment1);  
           MAP = myMAPF.getMap();  
           MAP.setMyLocationEnabled(true);  
           MAP.setMapType(GoogleMap.MAP_TYPE_HYBRID);  
           MAP.setOnMapClickListener(new OnMapClickListener() {  
                @Override  
                public void onMapClick(LatLng point) {  
                     // TODO Auto-generated method stub  
                     MAP.addMarker(new MarkerOptions().position(point).title(  
                               point.toString()));  
                }  
           });  
      }  
 }  

Without ActionBarsharelock:




 package com.djandroid.mapsv2;  
 import android.os.Bundle;  
 import android.support.v4.app.FragmentActivity;  
 import android.support.v4.app.FragmentManager;  
 import com.google.android.gms.maps.GoogleMap;  
 import com.google.android.gms.maps.GoogleMap.OnMapClickListener;  
 import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;  
 import com.google.android.gms.maps.SupportMapFragment;  
 import com.google.android.gms.maps.model.LatLng;  
 import com.google.android.gms.maps.model.Marker;  
 import com.google.android.gms.maps.model.MarkerOptions;  
 public class MainActivity extends FragmentActivity {  
      private GoogleMap MAP;  
      @Override  
      protected void onCreate(Bundle arg0) {  
           // TODO Auto-generated method stub  
           // setTheme(R.style.Theme_Sherlock);  
           super.onCreate(arg0);  
           setContentView(R.layout.activity_main);  
           FragmentManager myFM = getSupportFragmentManager();  
           SupportMapFragment myMAPF = (SupportMapFragment) myFM  
                     .findFragmentById(R.id.fragment1);  
           MAP = myMAPF.getMap();  
           MAP.setMyLocationEnabled(true);  
           MAP.setMapType(GoogleMap.MAP_TYPE_HYBRID);  
           MAP.setOnMapClickListener(new OnMapClickListener() {  
                @Override  
                public void onMapClick(LatLng point) {  
                     // TODO Auto-generated method stub  
                     MAP.addMarker(new MarkerOptions().position(point).title(  
                               point.toString()));  
                }  
           });  
      }  
 }  

You have to test the application on a real device as the emulator is not supported.
Enjoy....

Related Errors:


If we not add this in manifest:



 <uses-feature  
     android:glEsVersion="0x00020000"  
     android:required="true" />  


Then error is : java.lang.RuntimeException: Google Maps Android API only supports OpenGL ES 2.0 andabove. Please add <uses-feature android:glEsVersion="0x00020000" android:required="true" /> into AndroidManifest.xml



 <meta-data  
       android:name="com.google.android.maps.v2.API_KEY"  
       android:value="API_KEY" />  

then error is : java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml


Comments

  1. Hi Dhaval,

    I am getting unusal problem when i am adding Map.

    I need to add map in 3 Activity.

    1. I have added map, All the markers and custom Info Window.

    It is working fine.

    But In another activity when i need to use use map i created new activity with new map fragment and same procedure.

    When when i move to 2nd activity my activity containing map gets hanged and no crash log are even... I can just see application heap size going around 25 MB...

    Can you help me out to resolve the issue ??

    ReplyDelete
    Replies
    1. I ll try that and rpl you. but can you show me your design of 3 activity .. or you can mail me your code....

      I ll sure help you.

      Delete
  2. i doing in same way emulator not supporting ok but on device i installed googleplay service.apk file also by market, also set api key in manifest.xml file but on device showing blank only zoom control or gps sign showing map is not loading any idea same thing happening with googleservice api sample code.

    ReplyDelete
  3. try to re generate your MAP API key using : https://developers.google.com/maps/documentation/android/start

    ReplyDelete
  4. thanks got it the problem, i am looking for add proximity alert in MAPv2 , issue is that i am able to draw path between source & destination on map able to locate current location now i want set if current location not on drawing path then app will gave you notification that you are not on path any help,

    ReplyDelete
  5. my map is not displaying it just shows blank screen.
    java.lang.RuntimeException: Google Maps Android API only supports OpenGL ES 2.0 andabove
    after adding this
    the error remains the same.

    ReplyDelete
    Replies
    1. can you tell me in which deviceyou test your app??

      may b your device can't support openGL.

      Delete
  6. hi i've follow your tutorial but it doesn't work...i've error nullpointerexception in line MAP = myMAPF.getMap(); why???

    ReplyDelete
    Replies
    1. in your fargment you have write: supportmapfragment then in your activity you have to use supportmapfragment

      or

      if you have use mapfragment then you have to use mapfragment in activity

      i think this is your issue otherwise send me your code..

      Delete

  7. FragmentManager myFM = getSupportFragmentManager();
    SupportMapFragment myMAPF = (SupportMapFragment) myFM
    .findFragmentById(R.id.map);
    googleMap = myMAPF.getMap();

    I am using this code still i am getting nullpointerexception in line MAP = myMAPF.getMap();
    Please help.

    ReplyDelete
    Replies
    1. Hello vikasvname,

      can you show me logs?

      as well tell me you min & max sdk from Android manifest file

      Delete
    2. As a note, if you use Android support library to display a map, please make sure that your layout xml file that contains the map fragment should have an android:name attribute value to com.google.android.gms.maps.SupportMapFragment

      or if you are not using Android support library then use com.google.android.gms.maps.MapFragment

      Delete

Post a Comment

Popular posts from this blog

ANDROID - Adding ActionBar Navigation Tabs

Note: if you are develop App < 3.0 Android OS then use  ActionBarSherlock   support library. ActionBarSherlock is an extension of the  support library  designed to facilitate the use of the action bar design pattern across all versions of Android with a single API. Create new Android Project : in Main Activity package com.AlarmManager; import android.os.Bundle; import android.view.View; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.ActionBar.Tab; import com.actionbarsherlock.app.SherlockFragmentActivity; public class AlarmManagerActivity extends SherlockFragmentActivity { public static String ACTIVE_TAB = "activeTab"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { setTheme(R.style.Theme_Sherlock_Light_DarkActionBar); super.onCreate(savedInstanceState); // setContentView(R.lay

Android Material Design (with Design support Library) - 4

Introducing  Design Support Library  , to use Material design component in older version API - android 2.1 and above.  You’ll find a navigation drawer view, floating labels for editing text, a floating action button, snackbar, tabs, and a motion and scroll framework to tie them together. I have used my previous example, so its easy for demonstrate.  Note: Update your Android SDK support  repositories, support library if not updated i  used compileSdkVersion 'android-MNC' for Android M but you can change it to build in older API add  dependencies in build.gradle file compile 'com.android.support:appcompat-v7:22.2.0' compile 'com.android.support:design:22.2.0' compile 'com.android.support:support-v4:22.2.0' start with  navigation drawer , its very easy to use lets, design for drawer <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://

Android show Data from Sqlite DB into Grid View

Shaadi.com Matrimonials Shaadi.com Indian Matrimonials Your Main Activity class package com . Sqlite_grid_view ; import java . util . ArrayList ; import java . util . List ; import android . app . Activity ; import android . os . Bundle ; import android . util . Log ; import android . view . View ; import android . widget . AdapterView ; import android . widget . AdapterView . OnItemClickListener ; import android . widget . ArrayAdapter ; import android . widget . GridView ; import android . widget . TextView ; import android . widget . Toast ; public class AndroidSQLiteTutorialActivity extends Activity { private GridView gridView ; public static ArrayList < String > ArrayofName = new ArrayList < String >(); /** Called when the activity is first created. */ @ Override public void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ); setContentView ( R . l