geolocation and mapping in phonegap applications

68

Upload: ivano-malavolta

Post on 07-May-2015

10.178 views

Category:

Technology


2 download

DESCRIPTION

Geolocation and Mapping in PhoneGap applications This presentation has been developed in the context of the Mobile Applications Development course, DISIM, University of L'Aquila (Italy), Spring 2013. http://www.ivanomalavolta.com

TRANSCRIPT

Page 1: Geolocation and Mapping in PhoneGap applications
Page 2: Geolocation and Mapping in PhoneGap applications
Page 3: Geolocation and Mapping in PhoneGap applications

Page 4: Geolocation and Mapping in PhoneGap applications
Page 5: Geolocation and Mapping in PhoneGap applications
Page 6: Geolocation and Mapping in PhoneGap applications
Page 7: Geolocation and Mapping in PhoneGap applications

navigator.geolocation

Page 8: Geolocation and Mapping in PhoneGap applications

Page 9: Geolocation and Mapping in PhoneGap applications

geolocation

Page 10: Geolocation and Mapping in PhoneGap applications

getCurrentPosition(win, [fail], [options]);

Page 11: Geolocation and Mapping in PhoneGap applications

var id = watchPosition(win, [fail], [options]);

Page 12: Geolocation and Mapping in PhoneGap applications

clearWatch(watchID);

Page 13: Geolocation and Mapping in PhoneGap applications

Page 14: Geolocation and Mapping in PhoneGap applications

getCurrentPosition watchPosition

Page 15: Geolocation and Mapping in PhoneGap applications

Coordinates

Position

Page 16: Geolocation and Mapping in PhoneGap applications

Page 17: Geolocation and Mapping in PhoneGap applications

Page 18: Geolocation and Mapping in PhoneGap applications

Page 19: Geolocation and Mapping in PhoneGap applications

PositionError.PERMISSION_DENIED

PositionError.POSITION_UNAVAILABLE

PositionError.TIMEOUT

Page 20: Geolocation and Mapping in PhoneGap applications

var options = { maximumAge: 3000, timeout: 5000,

enableHighAccuracy: true };

navigator.geolocation.watchPosition(win, fail, options);

function win(pos) {

var el = „<div>Latitude: „ + pos.coords.latitude + '</div>');

el += „<div>Longitude: „ + pos.coords.longitude + '</div>');

el += „<div>timestamp: „ + pos.timestamp + '</div>');

$(„#block‟).html(el);

}

function fail(err) {

console.log(err.code);

}

Page 21: Geolocation and Mapping in PhoneGap applications
Page 22: Geolocation and Mapping in PhoneGap applications
Page 23: Geolocation and Mapping in PhoneGap applications
Page 24: Geolocation and Mapping in PhoneGap applications

google.maps.Map

Page 25: Geolocation and Mapping in PhoneGap applications

google.maps.Map(htmlElement, options);

• <div id=“map”></div>

Page 26: Geolocation and Mapping in PhoneGap applications

Page 27: Geolocation and Mapping in PhoneGap applications

Page 28: Geolocation and Mapping in PhoneGap applications

new google.maps.LatLng(42.3606,13.3772);

Page 29: Geolocation and Mapping in PhoneGap applications

var pt = new google.maps.LatLngBounds(

new google.maps.LatLng(57.8, 14.0),

new google.maps.LatLng(57.8, 14.0)

);

contains(pt), intersect(bounds), getCenter(),

union(bounds), etc.

Page 30: Geolocation and Mapping in PhoneGap applications

mapTypeId: google.maps.MapTypeId.ROADMAP

• ROADMAP

• SATELLITE

• HYBRID

• TERRAIN

Page 31: Geolocation and Mapping in PhoneGap applications

// in your JS file

var options = {

center: new google.maps.LatLng(-34.397, 150.644),

zoom: 8,

mapTypeId: google.maps.MapTypeId.ROADMAP

};

var map = new

google.maps.Map(document.getElementById(“map”),

options);

// somewhere in your HTML templates

<div id=“map”></div>

Page 32: Geolocation and Mapping in PhoneGap applications

– touchend

Page 33: Geolocation and Mapping in PhoneGap applications

addListener()

google.maps.event.addListener(obj, eventname, callback)

Page 34: Geolocation and Mapping in PhoneGap applications

addDOMListener(obj, eventname, callback)

addListener, obj

Page 35: Geolocation and Mapping in PhoneGap applications

var map = new

google.maps.Map(document.getElementById(“map”),

opt);

google.maps.event.addListener(map, 'click',

function(event) {

var marker = new google.maps.Marker({

position: event.latLng,

map: map

});

map.setCenter(marker.getPosition());

}

);

Page 36: Geolocation and Mapping in PhoneGap applications

Page 37: Geolocation and Mapping in PhoneGap applications

Page 38: Geolocation and Mapping in PhoneGap applications

Page 39: Geolocation and Mapping in PhoneGap applications

Page 40: Geolocation and Mapping in PhoneGap applications

marker = new google.maps.Marker({

// options

});

google.maps.event.addListener(marker, 'click',

callback);

Page 41: Geolocation and Mapping in PhoneGap applications

google.maps.Marker

– LatLng

– Map

– setMap()

– setMap() null

Page 42: Geolocation and Mapping in PhoneGap applications

•– google.maps.Animation.DROP

– google.maps.Animation.BOUNCE

setAnimation()

Page 43: Geolocation and Mapping in PhoneGap applications

Polyline LatLngs

Marker Polyline

Page 44: Geolocation and Mapping in PhoneGap applications

– LatLng

Page 45: Geolocation and Mapping in PhoneGap applications

var map; // the map object (initialization omitted here)

var coords = [

new google.maps.LatLng(37.772323, -122.214897),

new google.maps.LatLng(21.291982, -157.821856),

new google.maps.LatLng(-18.142599, 178.431),

new google.maps.LatLng(-27.46758, 153.027892)

];

var polyline = new google.maps.Polyline({

path: coords,

strokeColor: "#00FF00",

strokeOpacity: 1.0,

strokeWeight: 1

});

polyline.setMap(map);

Page 46: Geolocation and Mapping in PhoneGap applications

Page 47: Geolocation and Mapping in PhoneGap applications

google.maps.DirectionsService

Page 48: Geolocation and Mapping in PhoneGap applications

DirectionsService

DirectionsRequest

DirectionsService.route()

manageRoute

var dirService = new google.maps.DirectionsService();

var request = {

origin: ”…”,

destination: “…”, travelMode: google.maps.TravelMode.DRIVING

};

dirService.route(request, manageRoute);

Page 49: Geolocation and Mapping in PhoneGap applications
Page 50: Geolocation and Mapping in PhoneGap applications

– DirectionsRoute

Page 51: Geolocation and Mapping in PhoneGap applications
Page 52: Geolocation and Mapping in PhoneGap applications

• DirectionsLeg

• LatLngs

• LatLngBounds

Page 53: Geolocation and Mapping in PhoneGap applications

• DirectionsStep

• LatLng

LatLng

Page 54: Geolocation and Mapping in PhoneGap applications

• LatLng

LatLng

Page 55: Geolocation and Mapping in PhoneGap applications
Page 56: Geolocation and Mapping in PhoneGap applications
Page 57: Geolocation and Mapping in PhoneGap applications

google.maps.DistanceMatrixService

.getDistanceMatrix(options, callback)

Page 58: Geolocation and Mapping in PhoneGap applications

•–

•–

•–

•–

Page 59: Geolocation and Mapping in PhoneGap applications

• DistanceMatrixResponse

• DistanceMatrixStatus

Page 60: Geolocation and Mapping in PhoneGap applications

•–

•–

•–

•–

Page 61: Geolocation and Mapping in PhoneGap applications

var origin = “L‟Aquila, Italy";

var destination = “London, England";

var service = new google.maps.DistanceMatrixService();

service.getDistanceMatrix({

origins: [origin],

destinations: [destination],

travelMode: google.maps.TravelMode.DRIVING,

avoidHighways: false,

avoidTolls: false

}, callback);

function callback(response, status) {

if (status == google.maps.DistanceMatrixStatus.OK) {

var t = response.rows[0].elements[0].distance.text;

alert(t);

}

}

Page 62: Geolocation and Mapping in PhoneGap applications

42.362319,13.368514

Page 63: Geolocation and Mapping in PhoneGap applications

var geocoder = google.maps.Geocoder();

geocoder.geocode(options, callback);

•–

•–

Page 64: Geolocation and Mapping in PhoneGap applications

GeocoderResults

Page 65: Geolocation and Mapping in PhoneGap applications

geocoder = new google.maps.Geocoder();

var address = “via Vetoio 1, L‟Aquila”;

geocoder.geocode( { 'address': address}, callback);

function callback(results, status) {

if (status == google.maps.GeocoderStatus.OK) {

for(result in results) {

console.log(result.geometry.location);

}

} else {

console.log(status);

}

}

Page 66: Geolocation and Mapping in PhoneGap applications

Page 67: Geolocation and Mapping in PhoneGap applications
Page 68: Geolocation and Mapping in PhoneGap applications