CIS 470 Mobile App Development

Slides:



Advertisements
Similar presentations
Android Application Development Tutorial. Topics Lecture 4 Overview Overview of Sensors Programming Tutorial 1: Tracking location with GPS and Google.
Advertisements

Location based services
Location-Based Services: Part 2 (Google Maps)
Location & Maps.  Mobile applications can benefit from being location-aware, e.g.,  Routing from a current to a desired location  Searching for stores.
Map Applications.
Android Sensors & Async Callbacks Jules White Bradley Dept. of Electrical and Computer Engineering Virginia Tech
Programming Mobile Applications with Android September, Albacete, Spain Jesus Martínez-Gómez.
Location based services Using Google Maps v2 etc. in Android apps 1Location based services.
Mobile Programming Lecture 9 Bound Service, Location, Sensors, IntentFilter.
Location based services
CSS216 MOBILE PROGRAMMING Android, Chapter 8 Book: “Professional Android™ 2 Application Development” by Reto Meier, 2010 by: Andrey Bogdanchikov (
Android Accessing GPS Ken Nguyen Clayton State University 2012.
1 CMSC 628: Introduction to Mobile Computing Nilanjan Banerjee Introduction to Mobile Computing University of Maryland Baltimore County
Location. GPS Global Positioning System – At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within.
Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
Sensors – Part 2 SE 395/595. Location in Android LocationManager class – Configure providers and their listeners LocationListener class – Handles update.
TCS Internal Maps. 2 TCS Internal Objective Objective :  MAPS o Integration of Maps.
Cosc 4735 LocationAware API. Previous on … Before we looked at GPS location. – d-gpslocation.pptx
Google map v2.
Lecture 5: Location Topics: Google Play Services, Location API Date: Feb 16, 2016.
Location based services 1. Some location-based services available in Android Geo-coding – Address -> location Reverse geo-coding – Location -> address(es)
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
1. 2 Android location services Determining a device’s current location Tracking device movements Proximity alerts.
Location-Based Services. Objectives How to display Google Maps in your application How to control displayed maps How to perform geocoding and reverse.
CS499 – Mobile Application Development Fall 2013 Location & Maps.
Lecture 5: Location Topics: Google Play Services, Location API
Android Application Maps 1.
Lecture 5: Location Topics: Google Play Services, Location API.
Location-Based Services: Part 2 (Google Maps)
GUI Programming Fundamentals
Designing Apps Using The WebView Control
Sensors, maps and fragments:
Android – Read/Write to External Storage
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CS371m - Mobile Computing Maps.
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
Programming with Android: The Google Maps Library
CIS 470 Mobile App Development
Android Sensor Programming
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 493/EEC 492 Android Sensor Programming
CIS 470 Mobile App Development
CIS 470 Mobile App Development
Lecture 5: Location Topics: Google Play Services, Location API.
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CS 240 – Advanced Programming Concepts
Mobile Programming Dr. Mohsin Ali Memon.
CIS 470 Mobile App Development
External Services CSE 5236: Mobile Application Development
Android Sensor Programming
CIS 694/EEC 693 Android Sensor Programming
CIS 694/EEC 693 Android Sensor Programming
CIS 694/EEC 693 Android Sensor Programming
CA16R405 - Mobile Application Development (Theory)
CIS 470 Mobile App Development
External Services CSE 5236: Mobile Application Development
CIS 694/EEC 693 Android Sensor Programming
Presentation transcript:

CIS 470 Mobile App Development Lecture 13 Wenbing Zhao Department of Electrical Engineering and Computer Science Cleveland State University wenbing@ieee.org 11/13/2018 CIS 470: Mobile App Development

Location-Based Services Displaying Google Maps in your Android application Displaying zoom controls on the map Switching between the different map views Retrieving the address location touched on the map Performing geocoding and reverse geocoding Obtaining geographical data using GPS, Cell-ID, and WiFi triangulation Monitoring for a location 11/13/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development Displaying Maps When creating an app, need to select “Google Maps Activity” Create an app and name it LBS 11/13/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development Displaying Maps Also need to obtain the Maps API key (after you created your app) To get a Google Maps key, open the google_maps_api.xml file that was created in your LBS project Within this file is a link to create a new Google Maps key Copy and paste the link into your browser and follow the instructions Replace the YOUR_KEY_HERE placeholder in the google_maps_api.xml with your Google Maps key <resources> <!-- TODO: Before you run your application, you need a Google Maps API key. To get one, follow this link, follow the directions and press "Create" at the end: https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=AD:71:B7:6F:62:AA:7E:A6:30:10:AC:DC:CF:36:36:2C:9D:A7:C1:CB%3Bcom.wenbing.lbs Once you have your key (it starts with "AIza"), replace the "google_maps_key" string in this file. --> <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">AIzaSyCHapqQ2PtdWBdKwhHX8RmLlrkzaWb7rdE</string> </resources> 11/13/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development Zoom Control In activity_maps.xml, add maps:uiZoomControls=“true” <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" map:uiZoomControls="true" tools:context="com.wenbing.lbs.MapsActivity" /> 11/13/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development Changing Views By default, Google Maps is displayed in map view. You can change to satellite view programmatically by setting the map type: mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); 11/13/2018 CIS 470: Mobile App Development

Navigating to a Specific Location Programmatically show the map around a specific location using moveCamera() method public void onMapReady(GoogleMap googleMap) { mMap = googleMap; // Add a marker in Sydney and move the camera LatLng sydney = new LatLng(-34, 151); mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); // mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); 11/13/2018 CIS 470: Mobile App Development

Getting the Location that was touched To get the latitude and longitude of a point on the Google Map that was touched, you must set a onMapClickListener on mMap in onMapReady() method mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() { @Override public void onMapClick(LatLng point) { Log.d("DEBUG","Map clicked [" + point.latitude + " / " + point.longitude + "]"); Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault()); try { List<Address> addresses = geoCoder.getFromLocation(point.latitude,point.longitude,1); String add = ""; if (addresses.size() > 0) { for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++) add += addresses.get(0).getAddressLine(i) + "\n"; } Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); } LatLng p = new LatLng(point.latitude, point.longitude); mMap.moveCamera(CameraUpdateFactory.newLatLng(p)); } }); 11/13/2018 CIS 470: Mobile App Development

Geocoding and Reverse Geocoding Knowing latitude and longitude of a location, you can find out its address using a process known as reverse geocoding Google Maps in Android supports reverse geocoding via the Geocoder class The Geocoder object converts the latitude and longitude into an address using the getFromLocation() method 11/13/2018 CIS 470: Mobile App Development

Geocoding and Reverse Geocoding If you know the address of a location but want to know its latitude and longitude, you can do so via geocoding using getFromLocationName() method Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault()); try { List<Address> addresses = geoCoder.getFromLocationName( "empire state building", 5); if (addresses.size() > 0) { LatLng p = new LatLng((int) (addresses.get(0).getLatitude()), (int) (addresses.get(0).getLongitude())); mMap.moveCamera(CameraUpdateFactory.newLatLng(p)); } } catch (IOException e) { e.printStackTrace(); 11/13/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development Get Location Data Three means: GPS Cell tower triangulation WiFI In Android, use LocationManager Add permission in manifest To simulate GPS data received by the Android emulator, you use the Location Controls tool on the right-hand side of the emulator After starting the app, observe that the map on the emulator now animates to your current location. This indicates that the application has received the GPS data <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 11/13/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development Get Location Data import android.support.v4.app.FragmentActivity; import android.os.Bundle; import android.util.Log; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; import android.location.Address; import android.location.Geocoder; import android.widget.Toast; import java.io.IOException; import java.util.Locale; import java.util.List; import android.support.v4.app.ActivityCompat; import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.content.Context; 11/13/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { final private int REQUEST_COURSE_ACCESS = 123; boolean permissionGranted = false; LocationManager lm; LocationListener locationListener; private GoogleMap mMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); locationListener = new MyLocationListener(); if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_COURSE_ACCESS); return; }else{ permissionGranted = true; } 11/13/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development @Override public void onMapReady(GoogleMap googleMap) { // continue from previous slide if(permissionGranted) { lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); } // Add a marker in Sydney and move the camera LatLng sydney = new LatLng(-34, 151); mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() { @Override public void onMapClick(LatLng point) { Log.d("DEBUG","Map clicked [" + point.latitude + " / " + point.longitude + "]"); Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault()); try { List<Address> addresses = geoCoder.getFromLocation(point.latitude,point.longitude,1); String add = ""; if (addresses.size() > 0) { for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++) add += addresses.get(0).getAddressLine(i) + "\n"; } Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); } } }); } 11/13/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development @Override public void onPause() { super.onPause(); //---remove the location listener--- if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_COURSE_ACCESS); return; }else{ permissionGranted = true; } if(permissionGranted) { lm.removeUpdates(locationListener); } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case REQUEST_COURSE_ACCESS: if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { permissionGranted = true; } else { permissionGranted = false; } break; default: super.onRequestPermissionsResult(requestCode, permissions, grantResults); } } 11/13/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development private class MyLocationListener implements LocationListener { public void onLocationChanged(Location loc) { if (loc != null) { Toast.makeText(getBaseContext(), "Location changed : Lat: " + loc.getLatitude() + " Lng: " + loc.getLongitude(), Toast.LENGTH_SHORT).show(); LatLng p = new LatLng( (int) (loc.getLatitude()), (int) (loc.getLongitude())); mMap.moveCamera(CameraUpdateFactory.newLatLng(p)); mMap.animateCamera(CameraUpdateFactory.zoomTo(7)); } else { System.out.println("loc is null"); } Toast.makeText(getBaseContext(), "onLocation"+loc, Toast.LENGTH_SHORT).show(); } public void onProviderDisabled(String provider) { } public void onProviderEnabled(String provider) { } public void onStatusChanged(String provider, int status, Bundle extras) { } } } 11/13/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development Monitoring a Location Do something when the user (who carries the Android phone with the app) is getting close to a location using the addProximityAlert() method import android.app.PendingIntent; import android.content.Intent; import android.net.Uri; //---use the LocationManager class to obtain locations data--- lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); //---PendingIntent to launch activity if the user is within some locations--- PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.amazon.com")), 0); lm.addProximityAlert(37.422006, -122.084095, 5, -1, pendingIntent); 11/13/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development Exercises (required) Refactor the demo app: Add two buttons for the map view and satellite view Add another button for displaying current location When clicking a location on the map, add a marker and move the camera to that location Add a EditText view and a button such that when you enter a valid address, the camera will move to that location 11/13/2018 CIS 470: Mobile App Development