Skip to main content

GoogleMap V2 on Fragment Class

Google Map V2 on Fragment Class


The first thing that we will handle is the import of Google Map classes.
To get the Google Maps files we need to download the last version of
Google Play Services via the Android SDK Manager.


After you downloaded the Google Play Services
Import google play service library and also copy the lib to your  workspace 

you can found play service library here " \extras\google\google_play_services\libproject\google-play-services_lib "

Add the library to your project


import the support library this can be done very easily
using Eclipse: Right-Click you project and choose “Android Tools” and then choose
“Add Support Library…”:




  1.  import android.os.Bundle; 
  2.  import android.support.v4.app.Fragment; 
  3.  import android.view.LayoutInflater; 
  4.  import android.view.View; 
  5.  import android.view.ViewGroup; 
  6.   
  7.  import com.google.android.gms.maps.CameraUpdateFactory; 
  8.  import com.google.android.gms.maps.GoogleMap; 
  9.  import com.google.android.gms.maps.MapView; 
  10.  import com.google.android.gms.maps.MapsInitializer; 
  11.  import com.google.android.gms.maps.model.BitmapDescriptorFactory; 
  12.  import com.google.android.gms.maps.model.CameraPosition; 
  13.  import com.google.android.gms.maps.model.LatLng; 
  14.  import com.google.android.gms.maps.model.MarkerOptions; 
  15.   

  16.  public class MapFragment extends Fragment { 
  17.   
  18.      MapView mMapView; 
  19.      private GoogleMap googleMap; 
  20.   
  21.      @Override 
  22.      public View onCreateView(LayoutInflater inflater, ViewGroup container, 
  23.                               Bundle savedInstanceState) { 
  24.          // inflate and return the layout 
  25.          View v = inflater.inflate(R.layout.activity_map_fragment, container, 
  26.                  false); 
  27.          mMapView = (MapView) v.findViewById(R.id.mapView); 
  28.          mMapView.onCreate(savedInstanceState); 
  29.   
  30.          mMapView.onResume();// needed to get the map to display immediately 
  31.   
  32.          try { 
  33.              MapsInitializer.initialize(getActivity().getApplicationContext()); 
  34.          } catch (Exception e) { 
  35.              e.printStackTrace(); 
  36.          } 
  37.   
  38.          googleMap = mMapView.getMap(); 
  39.          // latitude and longitude 
  40.          double latitude = 17.385044; 
  41.          double longitude = 78.486671; 
  42.   
  43.          // create marker 
  44.          MarkerOptions marker = new MarkerOptions().position( 
  45.                  new LatLng(latitude, longitude)).title("Hello Maps"); 
  46.   
  47.          // Changing marker icon 
  48.          marker.icon(BitmapDescriptorFactory 
  49.                  .defaultMarker(BitmapDescriptorFactory.HUE_ROSE)); 
  50.   
  51.          // adding marker 
  52.          googleMap.addMarker(marker); 
  53.          CameraPosition cameraPosition = new CameraPosition.Builder(
  54.                  .target(new LatLng(17.385044, 78.486671)).zoom(12).build(); 
  55.          googleMap.animateCamera(CameraUpdateFactory 
  56.                  .newCameraPosition(cameraPosition)); 
  57.   
  58.          // Perform any camera updates here 
  59.          return v; 
  60.      } 
  61.   
  62.      @Override 
  63.      public void onResume() { 
  64.          super.onResume(); 
  65.          mMapView.onResume(); 
  66.      } 
  67.   
  68.      @Override 
  69.      public void onPause() { 
  70.          super.onPause(); 
  71.          mMapView.onPause(); 
  72.      } 
  73.   
  74.      @Override 
  75.      public void onDestroy() { 
  76.          super.onDestroy(); 
  77.          mMapView.onDestroy(); 
  78.      } 
  79.   
  80.      @Override 
  81.      public void onLowMemory() { 
  82.          super.onLowMemory(); 
  83.          mMapView.onLowMemory(); 
  84.      } 
  85.  } 

Android Manifest File 


  <permission 
        android:name="com.example.googmap.permission.MAPS_RECEIVE"   
        android:protectionLevel="signature"/>
    
<uses-permission android:name="com.example.googmap.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

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



 <!-- Inside Application File  -->
        <meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="AIzaSyBLnnFi3l7xzyDsGY4ynWoPfsHKLBXhTLs"/> 
           
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />




Create and Obtain API Key for Google Maps Android API v2 service



Google Developers Console 

https://console.developers.google.com/project/


Step1. Create a Project 
Step2. Enable Google Map Service for android 



Step3. Create new Key

Step4. Create android key


Step5. Generate SHA Key from android Pc and past it along with package name 



SHA1 certificate fingerprint 


for Windows 

Go to your java bin directory via the cmd:
C:\Program Files\Java\jdk1.7.0_71\bin>
Now type in the below command in your cmd:
keytool -list -v -keystore c:\users\your_user_name\.android\debug.keystore -alias androiddebugkey -storepass android -keypass android









Comments

Popular posts from this blog

Bluetooth Chat Example

Manifest File <manifest xmlns:android="http://schemas.android.com/apk/res/android"       package="com.example.android.BluetoothChat"       android:versionCode="1"       android:versionName="1.0">     <uses-sdk minSdkVersion="7" />     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />     <uses-permission android:name="android.permission.BLUETOOTH" />     <application android:label="@string/app_name"                  android:icon="@drawable/app_icon" >         <activity android:name=".BluetoothChat"                   android:label="@string/app_name"                   android:configChanges="orientation|keyboardHidden">             <intent-filter> ...

Custom TextView With Images in Android

Create a custom XML Tag with image and text 1) Class extends Textview import java . util . regex . Matcher ; import java . util . regex . Pattern ; import android . content . Context ; import android . text . Spannable ; import android . text . style . ImageSpan ; import android . util . AttributeSet ; import android . util . Log ; import android . widget . TextView ; public class TextViewWithImages extends TextView { public TextViewWithImages ( Context context , AttributeSet attrs , int defStyle ) { super ( context , attrs , defStyle ); } public TextViewWithImages ( Context context , AttributeSet attrs ) { super ( context , attrs ); } public TextViewWithImages ( Context context ) { super ( context ); } @Override public void setText ( CharSequence text , BufferType type ) { Spannable s = getTextWithImages ( getContext (), text ); super . setText...

AcceleroMeter Sensors with SurfaceMovements on Canvas

MainActivity.java import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; public class MainActivity extends Activity implements SensorEventListener { float x, y, sensorX, sensorY; Bitmap ball; SensorManager sm; Sensor s; MyBringBackSurface ourSurfaceView; /**  * Canvas Movement  */ public class MyBringBackSurface extends SurfaceView implements Runnable {     SurfaceHolder ourHolder;     Thread ourThread = null;     boolean isRunning = false;     public MyBringBackSurface(Context context) {         super(context)...