Pages

Android - Google MAP V2 PART 2

Tuesday, 19 February 2013


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


12 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