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 - Google MAP V2 PART 3 (add Polyline)

Download Full Code  of  PART-1 , 2 , 3 , 4 Before go ahead Please look in to PART-1 & PART-2 I have just modify Main Activity class: package com.djandroid.mapsv2; import android.graphics.Color; 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; import com.google.android.gms.maps.model.Polyline; import com.google.android.gms.maps.model.PolylineOptions; public class MainActivity extends FragmentActivity { private GoogleMap MAP; private boolean markClic...

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

Connecting mysql Database in ANDROID using PHP & JSON

 To implement this tutorial you should have basic knowledge of how to run PHP script and start server.  If we talk about client-server architecture, client is Android device and in server side there is a combination of PHP Script and MySQL. In short, PHP Script sits in middle as shown in image. Lets suppose that we have a MySQL database named Employee, and a table int created, with the following SQL: CREATE TABLE `employee` (   `emp_id` int(11) NOT NULL auto_increment,   `emp_name` varchar(100) NOT NULL,   PRIMARY KEY  (`emp_id`) ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; The PHP code will be very simple: Now Create Android Project :  The Android part is only a bit more complicated: -use a HttpPost to get the data -convert response to string -parse JSON data in to List In Your First Activity : O/P :