Java tutorial
/******************************************************************************* * Copyright (C) 2015 Kristian Sloth Lauszus. All rights reserved. * * This software may be distributed and modified under the terms of the GNU * General Public License version 2 (GPL2) as published by the Free Software * Foundation and appearing in the file GPL2.TXT included in the packaging of * this file. Please note that GPL2 Section 2[b] requires that all works based * on this software must also be made publicly available under the terms of * the GPL2 ("Copyleft"). * * Contact information * ------------------- * * Kristian Sloth Lauszus * Web : http://www.lauszus.com * e-mail : lauszus@gmail.com ******************************************************************************/ package com.lauszus.launchpadflightcontrollerandroid.app; import android.Manifest; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.location.Criteria; import android.location.Location; import android.location.LocationManager; import android.support.v4.app.ActivityCompat; import android.widget.Toast; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; public class MapFragment extends SupportMapFragment { private GoogleMap mMap; private final int GPS_PERMISSIONS_REQUEST = 0; @Override public void onResume() { super.onResume(); LocationManager locationManager = (LocationManager) getActivity() .getSystemService(Context.LOCATION_SERVICE); if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { new AlertDialog.Builder(getActivity()) .setMessage("Your GPS seems to be disabled, do you want to enable it?").setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(final DialogInterface dialog, final int id) { startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); } }).setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(final DialogInterface dialog, final int id) { dialog.cancel(); Toast.makeText(getActivity(), "GPS must be on in order to use this application!", Toast.LENGTH_LONG).show(); getActivity().finish(); } }).create().show(); } else { setUpMapIfNeeded(); if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { Location location = locationManager .getLastKnownLocation(locationManager.getBestProvider(new Criteria(), false)); if (location != null) { LatLng myLocation = new LatLng(location.getLatitude(), location.getLongitude()); mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(myLocation, 19.0f)); } } } } private void enableMyLocationLayer() { if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) mMap.setMyLocationEnabled(true); } private void setUpMapIfNeeded() { // Do a null check to confirm that we have not already instantiated the map. if (mMap == null) { mMap = getMap(); // Obtain the map // Check if we were successful in obtaining the map. if (mMap != null) { mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) enableMyLocationLayer(); else requestPermissions(new String[] { Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION }, GPS_PERMISSIONS_REQUEST); mMap.getUiSettings().setCompassEnabled(true); mMap.getUiSettings().setAllGesturesEnabled(true); } else { Toast.makeText(getActivity(), "Google Maps is not available!", Toast.LENGTH_LONG).show(); getActivity().finish(); } } } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case GPS_PERMISSIONS_REQUEST: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Permission was granted, yay! Toast.makeText(getActivity(), "Location permission granted", Toast.LENGTH_SHORT).show(); enableMyLocationLayer(); } else { // Permission denied, boo! Toast.makeText(getActivity(), "Location permission required!", Toast.LENGTH_LONG).show(); getActivity().finish(); } } } } }