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

Google Map v2 signed map isssue fixed

Hi Step 1: Develop Map Application  using android studio Step 2: For Map viewing we need to have google api key .in order to get api key we need to register application in google api console  https://console.developers.google.com/apis/ Step 3; For getting SHA1 Key please follow the steps            1) Go the java installed folder location , open cmd                 Eg: C:\Program Files\Java\jdk1.7.0_04\bin            2)  Type                   Windows keytool -list -v -keystore "CHANGE WITH OUR KEYSTORE LOC" -alias ALIASNAME -storepass PASSWORD -keypass PASSWORD                   Mac & Linux  keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android Ref:  http://www.truiton.com/2015/04/obtaining-sha1...

Store Image to Google Cloud Store using Google App engine java

Here is sample code for uploading image files to cloud storage bucket in google cloud platform Step 1 Create a google cloud project in cloud (https://console.cloud.google.com/)  enable cloud storage Step 2: Create a new web application project I am using Eclipse with Java for App engine Project Step 3: create a POST web service method the below code for upload image to the existing bucket in google cloud and the uploaded image url will be returned Code   @Path("/addres")  @POST       @Consumes(MediaType.MULTIPART_FORM_DATA)    public String addRestaurant( @FormDataParam("imgname")  String imagename,            @FormDataParam("file") InputStream uploadedInputStream,         @FormDataParam("file") FormDataContentDisposition restImages     ) throws IOException  {   try { // Create your service object Storage storage = St...