Mobile location-based apps · Location-Based Services 1. Location-based: 80% of data 2. Service (or...
Transcript of Mobile location-based apps · Location-Based Services 1. Location-based: 80% of data 2. Service (or...
Mobile location-based apps
University of TartuJaak Laineste, Oct. 2017
Location-based service
overview
Part 1
Jaak Laineste
• GIS/LBS experience
– 22 years in GIS/mapping, 17 years in LBS
– Regio/Reach-U, Mobile operator LBS globally
– Nutiteq: 2006-2016
– CARTO since 2016
• CARTO and mobile development
– CARTO Maps SDK – native maps API
– CARTO Builder and Engine – Location Intelligence
platform
Location-Based Services
1. Location-based: 80% of data
2. Service (or mobile application)
3. Mobile technologies (phones, networks)
4. Mobile positioning
• LBS is universial technology
– Can be aspect of any app type: info, game,
tool, social...
Android app with mapPart 2
What can be done
• Display map
– MapView API – interactive map:
Google Maps, 2D/2.5D, online
– Add clickable points to map
(annotations, clusters)
– Add overlays (rasters)
– Show user GPS location
• Geocoding
– android.location.Geocoder API,
online
• Reverse-geocoding
– android.location.Geocoder
getFromLocation()
• Calculate distance between
objects
– Location.distanceTo() method
• Find Point in area (and other
georelations)
– Spatial-enabled GIS engine:
PostGIS in server or Spatialite in
phone
• Routing – find optimal path in
graph
– Google online API, other on-line
routing engines
– Spatialite routing, in small area
– Graphhopper offline – open
source
• Navigation
– Turn by turn – Telenav, HERE
SDK
• Offline
– Maps, Routing, Geocoding, Data
– CARTO SDK
Practical coding on Android
1. Adding MapView to app
2. Add layers and objects to map
– Make pin clickable
3. Manipulate with map view
4. Get and show user location
5. Using geo back-end service
– CARTO service
If you get stuck
• Code snippets
– http://bit.do/ut-lbs
• Guides:
– https://carto.com/docs/carto-engine/mobile-
sdk
• Working sample
– https://github.com/CartoDB/mobile-android-
samples/
Add Maps SDK to your project
1. Create new project, with single view
– Minimum SDK : 15, Phone and Tablet
– Blank activity, with Compatibility
2. Automatic CARTO SDK dependency to
MyApplication project build.gradle , add to
existing lines
dependencies {
compile 'com.carto:carto-mobile-sdk:4.1.0-rc.3@aar'
}
3. Sync gradle
http://bit.do/ut-lbs
Add MapView to Layout
• Make sure your AndroidManifest.xml defines
INTERNET permission
• Modify Layout:
– Change TextView to com.carto.ui.MapView
http://bit.do/ut-lbs
Initial code into MainActivity
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 1. The initial step: register your license. This must be done before using MapView!
MapView.registerLicense("YOUR_LICENSE_KEY", getApplicationContext());
// Create map view
MapView mapView = (MapView) this.findViewById(R.id.mapView);
// Create base layer. Use vector style from assets
CartoOnlineVectorTileLayer baseLayer = new CartoOnlineVectorTileLayer(CartoBaseMapStyle.CARTO_BASEMAP_STYLE_VOYAGER);
// Add layer to map
mapView.getLayers().add(baseLayer);
}
http://bit.do/ut-lbs
Data models
• Raster
– PNG for maps, lossless
– JPG for aerials
– GeoTIFF, coverages
– Key parameters: bitmap resolution, size, channels
• Vector
– Base objects: points, lines, polygons
– Collections: multi-polygon, multi-line, multi-point
– Attributes (fields): text/boolean/numeric/binary etc
– Texts on map - labels from attributes
– Layer – same as „table“ in DB
– Special cases: topological models, graphs, 3D vectors
GIS Layers
Other data models
• Elevation models
– DEM – Digital Elevation Model
– TIN - triangulated irregular network
• 3D worlds
– Collada, X3D, KMZ, 3D vector tiles
– CityGML, IndoorGML
• Point clouds
– Lidar – laser measurements
Hillshade from DEM
TIN
Point cloud
3D model
GIS layers in a map
• One base layer
– Background map: Google, OpenStreetMap etc
– Raster or vector-based
• Overlay layer(s)
– Points of Interest, markers
– GPS location – dynamic info
– Lines, Polygons, Points
– Clustered points
– 3D objects
Add pin to map
// Initialize a local vector data source
Projection proj = mapView.getOptions().getBaseProjection(); LocalVectorDataSource vectorDataSource1 = new LocalVectorDataSource(proj);
// Create marker styleMarkerStyleBuilder markerStyleBuilder = new MarkerStyleBuilder();markerStyleBuilder.setSize(30);MarkerStyle sharedMarkerStyle = markerStyleBuilder.buildStyle();// Add marker to the local data sourceMapPos mapPos = proj.fromWgs84(new MapPos(13.38933, 52.51704));Marker marker1 = new Marker(mapPos, sharedMarkerStyle);
vectorDataSource1.add(marker1);
// Create a vector layer with the previously created data sourceVectorLayer vectorLayer1 = new VectorLayer(vectorDataSource1);// Add the previous vector layer to the mapmapView.getLayers().add(vectorLayer1);
http://bit.do/ut-lbs
Make map pin interactive
1. Add metadata to object
marker1.setMetaDataElement("id","My PIN");
2. Add Listener
http://bit.do/ut-lbs
Projections
Coordinate systems
• Geographical – spherical
– Units: Latitude and Longitude
– Based on an ellipsoid, e.g. WGS-84
– Datums, also WGS84 for GPS
– DMS for display, decimal degrees for programming
• Projected - cartesian
– Units: usually meters
– 1000+ named projections, mostly for local regions
– Reduce distortions: keep angles, distances, areas
equal
Geographical coordinate space
Cartesian (projected) coordinate
space
Different projections
• Extreme examples
– http://www.jasondavies.com/maps/
• In real life
– Plate Carre, Latitude/Longitude, WGS84
(EPSG:4326)
– Web map Spherical Mercator (EPSG:3857)
– Mercator
– UTM with zones
– Robinson – equal-area world map
– Lambert Conformal Conic, L-EST (EPSG:3301)
Robinson
Mercator
Short and long projection
description
EPSG codes: 4-5 digit numbers
• EPSG:4326 – WGS84
• EPSG:3301 – Estonian system
• EPSG:3857 – „Google web Mercator“
– A.k.a EPSG:900913
<EPSG:3301>
PROJCS["Estonian Coordinate System of 1997",
GEOGCS["EST97",
DATUM["Estonia_1997",
SPHEROID["GRS 1980",6378137,298.257222101,
AUTHORITY["EPSG","7019"]],
TOWGS84[0,0,0,0,0,0,0],
AUTHORITY["EPSG","6180"]],
PRIMEM["Greenwich",0,
AUTHORITY["EPSG","8901"]],
UNIT["degree",0.01745329251994328,
AUTHORITY["EPSG","9122"]],
AUTHORITY["EPSG","4180"]],
UNIT["metre",1,
AUTHORITY["EPSG","9001"]],
PROJECTION["Lambert_Conformal_Conic_2SP"],
PARAMETER["standard_parallel_1",59.33333333333334],
PARAMETER["standard_parallel_2",58],
PARAMETER["latitude_of_origin",57.51755393055556],
PARAMETER["central_meridian",24],
PARAMETER["false_easting",500000],
PARAMETER["false_northing",6375000],
AUTHORITY["EPSG","3301"],
AXIS["Y",EAST],
AXIS["X",NORTH]]
View manipulation and some
Options// Set view
mapView.setFocusPos(proj.fromWgs84(new MapPos(13.38933, 52.51704)), 0); mapView.setZoom(10, 0); // zoom 2, duration 0 seconds (no animation)mapView.setMapRotation(0, 0);mapView.setTilt(90, 0);
// Some options
mapView.getOptions().setRotatable(false); // make map not rotatable
mapView.getOptions().setTileThreadPoolSize(4); // faster tile downloading
mapView.getOptions().setZoomRange(new MapRange(0, 14));
// Use MapView coordinate system like this:
mapView.getOptions().setPanBounds(new MapBounds(new MapPos(2336596.00, 8398607.00), new MapPos(2561461.75, 8626954.00)));
http://bit.do/ut-lbs
zoom = 0 zoom = 1 zoom = 2
X=0Y=0
X=0Y=0
X=0Y=1
X=1Y=1
X=1Y=0
y
x
X=0Y=0
X=0Y=1
X=1Y=1
X=1Y=0
X=0Y=2
X=0Y=3
X=1Y=3
X=1Y=2
X=2Y=0
X=2Y=1
X=3Y=1
X=3Y=0
X=2Y=2
X=2Y=3
X=3Y=3
X=3Y=2
Web map tiling system (OSM)
X=150524Y=78432Zoom=18
User location on map
• Android Location API
– GNSS (GPS, GLONASS, Galileo) + wifi + mobile network cell
• Alternative positioning methods
– External GPS
– Mobile Positioning
– Indoor positioning
• Beacons
• Wifi
• Bluetooth
• Magnetic variations
Location API in Android
• Location Provider
– Selected automatically based on set requirements: Cell-id,WiFI or GNSS
• Features
– Listen for updates – most common
– Last known location (cached) – not suggested
– Proximity alerts – not guaranteed
• Best strategy depends on app
– http://developer.android.com/guide/topics/location/obtaining-user-location.html
Using Android location API code1. Define permission in AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
2. Ask runtime permissions
– Needed for Android 6.0+
3. Add a Polygon (circle) to map
– Can reuse Layer+DataSource of pins
– Make it a member, not local variable
4. Define LocationListener
– Get location updates, always async
– Get provider status updates
5. Define LocationManager with location Criteria
– Time and accuracy criteria
– Do not consume too much power
http://bit.do/ut-lbs
Add a circle to map
// style for GPS My Location circlePolygonStyleBuilder polygonStyleBuilder = new PolygonStyleBuilder();polygonStyleBuilder.setColor(new Color(0xAAFF0000));PolygonStyle gpsStyle = polygonStyleBuilder.buildStyle();
MapPosVector gpsCirclePoses = new MapPosVector();final Polygon locationCircle = new Polygon(gpsCirclePoses, gpsStyle);
// initially empty and invisiblelocationCircle.setVisible(false);
vectorDataSource.add(locationCircle);
http://bit.do/ut-lbs
Define locationlistener
final LocationListener locationListener = new LocationListener(){
@Overridepublic void onLocationChanged(Location location) {
Log.debug("GPS onLocationChanged "+location);if (locationCircle != null) {
locationCircle.setPoses(createLocationCircle(location, proj));locationCircle.setVisible(true);mapView.setFocusPos(proj.fromWgs84(
new MapPos(location.getLongitude(), location.getLatitude())), 0.5f);
}}
@Overridepublic void onStatusChanged(String s, int i, Bundle bundle) {}
@Overridepublic void onProviderEnabled(String s) {}
@Overridepublic void onProviderDisabled(String s) {}
};
http://bit.do/ut-lbs
Define LocationManagerLocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// user has maybe disabled location services / GPSif(locationManager.getProviders(true).size() == 0){
Toast.makeText(this, "Cannot get location, no location providers enabled. Check device settings",Toast.LENGTH_LONG).show();}
// use all enabled device providers with same parametersfor(String provider : locationManager.getProviders(true)){
Log.debug("adding location provider " + provider);locationManager.requestLocationUpdates(provider, 1000, 50, locationListener);
}
http://bit.do/ut-lbs
Create circular polygonprivate MapPosVector createLocationCircle(Location location, Projection proj) {
// number of points of circleint N = 50;int EARTH_RADIUS = 6378137;
float radius = location.getAccuracy();double centerLat = location.getLatitude();double centerLon = location.getLongitude();
MapPosVector points = new MapPosVector();for (int i = 0; i <= N; i++){
double angle = Math.PI*2*i/N;double dx = radius*Math.cos(angle);double dy = radius*Math.sin(angle);double lat = centerLat + (180/Math.PI) * (dy/ EARTH_RADIUS);double lon = centerLon + (180/Math.PI) * (dx/ EARTH_RADIUS)/Math.cos(centerLat * Math.PI/180);points.add(proj.fromWgs84(new MapPos(lon, lat)));
}return points;
}
http://bit.do/ut-lbs
Indoor LBS
• 90% time people are indoors
– Retail, Safety
• Indoor maps
– Google Maps, project Tango
– HERE: 90K buildings, 71 countries in 2014
– Micello (15K), aisle411 (12K)
– 3D technologies R&D Tartu: Wayfinder
– Sanborn SPIN: https://www.youtube.com/watch?v=UX9nOh3XiJs
• Indoor Positioning
– IndoorAtlas, Indoor.rs etc
• Technologies
– iBeacon positioning – BT LE
– Standards: IndoorGML, OSM Simple 3D, i-locate.eu
Map data modelsAdding online (or offline) data to map
Common GIS file formats• Shapefile (ESRI) – most common
– 4+ files per layer: .shp, .dbf etc
– One geometry type per layer allowed
– No style information, pure geometry
– Optional: projection file .prj, spatial index, charset
• KML (Google, open standard)
– Vector data, includes styles
– Special data types: 3D data (Collada), linked data, visual coverages
– Can have only WGS84 coordinates
– Can be KMZ – zipped file
• Other formats
– GeoJSON and TopoJSON – web/Javascript-friendly
– GeoPackage: SQLite binary format, vector and raster
– SpatiaLite: vector and raster data, SQLite. Any projection, no styles.
– Text files (CSV) with coordinates or addresses
– Every commercial GIS has own format(s)
– Free converter: http://www.gdal.org/ogr/ogr_formats.html
Spatial SQL database basics
• Special column data types(s):
– Geometry, Point, Polygon ...
• Geographical indexing
– Usually R-Tree, based on object bounds
• Geographical functions:
– Manipulations, relations, queries etc
• Metadata table:
– Defines coordinate system, data type for every
Geometry column
Geometry primitives in WKT (2D)
Multipart geometries in WKT (2D)
PostGIS sample: spatial query
• Be careful with :
– Coordinate systems – distances in meters vs
degrees
– Big data: use two-phase filters for spatial
indexes
PostGIS : spatial query
OpenStreetmapAs key data source
OpenStreetMap (OSM)
• Free and open data
– Vector data in 2D
– Streets, roads, buildings, amenities etc
• A lot of services
– Map images (tiles), geocoders, routers etc
– Special views: opencyclemap, openpistemap etc
• Everyone can improve the map
– www.openstreetmap.org
– www.maakaart.ee – in Estonian
ID – web-based OSM editor
JOSM – OSM main editor
OSM advantages
• Free and open to use
– No advertising, restrictions
• Vector data access
– Custom styles for mapping
– Own filters of data on map (layers)
– Interactive data overlays (POI layers)
– Advanced services: routing, search, analysis, ...
• Fast and easy updates
– Find error – go fix it yourself!
– Note: follow community guidelines
Commercial map data
• Global vendors
– Vector: HERE, TomTom, AND
– Aerial/Satellite: DigitalGlobe, Planet Labs
• Local vendors
– Estonia: Regio, Maa-amet
– In almost every country, quite detailed
• Specifics
– Technically quite flexible
– Usually quite expensive
CARTO in mobile app
CARTO intro• Cloud-based geo data storage
– Like google drive, dropbox etc, but with geo dimension and logic
• Spatial data view
– Map visualizations (thematic maps)
– Edit geometries on map
• Analytics
– Calculate derived data, create models etc
• Developer APIs
– SQL API
– Map tile API – raster map
– CARTO.JS for web
– Mobile SDK
Demo data for your app
• http://nutiteq.carto.com
– Estonian speed cameras from poiplaza.com
– Estonian villages and counties (külad ja maakonnad)
– About 1M OpenStreetMap amenities from Europe
• Datasets and Maps
– Table and Map views
– Thematic map for speed cameras
– Joined dataset for speed cameras and admin borders
Load and show speed cameras
• CARTO SQL API
– <API CALL> link under dataset
• Combine data from several tables:
SELECT sc.*,ay.a_nimi,ay.m_nimi,ay.o_nimi
FROM ee_speedcams sc,
asustusyksus_2017_reformitud ay
WHERE
st_contains(ay.the_geom,sc.the_geom)
Load GeoJSON, create Markers
• Use Thread for multi-threading
• Define service, set account
final CartoSQLService service = new CartoSQLService();service.setUsername("nutiteq");
• Load GeoJSON from GeometryCollectionThread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
PointStyleBuilder pointStyleBuilder = new PointStyleBuilder();pointStyleBuilder.setColor(new Color(android.graphics.Color.MAGENTA));pointStyleBuilder.setSize(3);
String query = "SELECT sc.*,ay.a_nimi,ay.m_nimi,ay.o_nimi FROMee_speedcams sc, asustusyksus_2017_reformitud ay WHEREst_contains(ay.the_geom,sc.the_geom)";
FeatureCollection features = service.queryFeatures(query, projection);
source.addFeatureCollection(features,pointStyleBuilder.buildStyle());
} catch (IOException e) {e.printStackTrace();
}}
});
thread.start();
http://bit.do/ut-lbs
Useful geo tools
Free GIS tools
• Desktop tools – mapping, analysis, processing
– QGIS - freeware
– Google Earth Pro - view/create KML, free
– Trimble SketchUp – create 3D models
• Databases
– PostGIS – Postgre add-on.
– GeoPackage or Spatialite – SQLite extension (not
enabled on Android/iOS)
• Processing
– GDAL/OGR – raster and vector library, command-line
converters
Free GIS tools for developers
• PostGIS
– Postgre SQL server extension
– Raster features, Topology
• JTS
– Java Topology Suite, ports in C (GEOS) etc
– Lot of complex algorithms, geometries, graphs, geographical indexes
• Proj.4
– C-based, has few ports. Proj4J for Java
– Hundreds of predefined projections
• Web platforms
– GeoDjango (Python), GeoNode (Node.js), GeoTools(Java)
Some GeoWebServices SaaS
• CARTO.com
– Hosted PostGIS
– Location Intelligence
• Other similars
– Mapbox – Google Maps clone
– GISCloud.com
– ESRI ArcGIS Online - commercial
• OpenStreetMap.org
– Only for community-created data
– Content requirements: verifyable objects etc
• Github
– GeoJSON and TopoJSON support
– geojson.io – GeoJSON editor
• Google Fusion Tables
– General data table API service
– Includes basic geo-features
Some cool future geo things
• Cesium JS – 3D in web browser
• D3 JS – cool rendering
• MapD – GPU-based superfast database
• Augmented Reality maps?
– https://drive.google.com/open?id=0B3Rm1qU6
KnySN0lEXzAtOFZQeU0