RSS2.0

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


9 comments:

Rakesh Gondaliya said...

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 ??

Dhaval Sodha Parmar said...

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.

Imran Khan said...

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.

Dhaval Sodha Parmar said...

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

Imran Khan said...

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,

Nitesh Sirohi said...

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.

Dhaval Sodha Parmar said...

can you tell me in which deviceyou test your app??

may b your device can't support openGL.

Unknown said...

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

Dhaval Sodha Parmar said...

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..

Post a Comment