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.
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…”:
using Eclipse: Right-Click you project and choose “Android Tools” and then choose
“Add Support Library…”:
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import com.google.android.gms.maps.CameraUpdateFactory;
- import com.google.android.gms.maps.GoogleMap;
- import com.google.android.gms.maps.MapView;
- import com.google.android.gms.maps.MapsInitializer;
- import com.google.android.gms.maps.model.BitmapDescriptorFactory;
- import com.google.android.gms.maps.model.CameraPosition;
- import com.google.android.gms.maps.model.LatLng;
- import com.google.android.gms.maps.model.MarkerOptions;
- public class MapFragment extends Fragment {
- MapView mMapView;
- private GoogleMap googleMap;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // inflate and return the layout
- View v = inflater.inflate(R.layout.activity_map_fragment, container,
- false);
- mMapView = (MapView) v.findViewById(R.id.mapView);
- mMapView.onCreate(savedInstanceState);
- mMapView.onResume();// needed to get the map to display immediately
- try {
- MapsInitializer.initialize(getActivity().getApplicationContext());
- } catch (Exception e) {
- e.printStackTrace();
- }
- googleMap = mMapView.getMap();
- // latitude and longitude
- double latitude = 17.385044;
- double longitude = 78.486671;
- // create marker
- MarkerOptions marker = new MarkerOptions().position(
- new LatLng(latitude, longitude)).title("Hello Maps");
- // Changing marker icon
- marker.icon(BitmapDescriptorFactory
- .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
- // adding marker
- googleMap.addMarker(marker);
- CameraPosition cameraPosition = new CameraPosition.Builder()
- .target(new LatLng(17.385044, 78.486671)).zoom(12).build();
- googleMap.animateCamera(CameraUpdateFactory
- .newCameraPosition(cameraPosition));
- // Perform any camera updates here
- return v;
- }
- @Override
- public void onResume() {
- super.onResume();
- mMapView.onResume();
- }
- @Override
- public void onPause() {
- super.onPause();
- mMapView.onPause();
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- mMapView.onDestroy();
- }
- @Override
- public void onLowMemory() {
- super.onLowMemory();
- mMapView.onLowMemory();
- }
- }
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
Post a Comment