39
Marcos Lin @marcoseu HTML5 Code Show Roma Rome, 24 Apr 2014 Playing with Maps using AngularJS

Playing with Maps using AngularJS

Embed Size (px)

DESCRIPTION

Slide used in the HTML5 CodeShow Roma #9. The talk covers basic Maps API following by the use of AngularJS to create a Maps application. The source code for live demo can be found under the "demo" directory in the github repository.

Citation preview

Page 1: Playing with Maps using AngularJS

Marcos Lin@marcoseu

HTML5 Code ShowRoma

Rome, 24 Apr 2014

Playing with Mapsusing AngularJS

Page 2: Playing with Maps using AngularJS
Page 3: Playing with Maps using AngularJS

http://www.energy-car.com

Catalyst forElectric

Vehicle Market

Page 4: Playing with Maps using AngularJS

http://goo.gl/39bCsm

@

Page 5: Playing with Maps using AngularJS

https://github.com/marcoslin/angularAMD

Page 6: Playing with Maps using AngularJS

Let’s Talk About MapsGoogle Maps

OpenStreetMap

Apple Maps

Yahoo! Maps

Bing Maps

Nokia Here

..

http://en.wikipedia.org/wiki/Comparison_of_web_map_services

Page 7: Playing with Maps using AngularJS

https://developers.google.com/maps/documentation/javascript/

Google Maps Javascript API

Page 8: Playing with Maps using AngularJS

http://dev.openlayers.org/apidocs

Open Layers

Page 9: Playing with Maps using AngularJS

https://developer.mozilla.org/en-US/Apps/Build/gather_and_modify_data/Plotting_yourself_on_the_map

Plotting yourself on the map

Page 10: Playing with Maps using AngularJS

http://goo.gl/FVhr5L

Page 11: Playing with Maps using AngularJS

http://marcoslin.github.io/sample-geo-angular/

Page 12: Playing with Maps using AngularJS

OpenStreetMap

Page 13: Playing with Maps using AngularJS

Google Maps

Page 14: Playing with Maps using AngularJS

Hybrid Map

Page 15: Playing with Maps using AngularJS

Show Me the Code!!!

Page 16: Playing with Maps using AngularJS

Map Quickie

Page 17: Playing with Maps using AngularJS

Live Code: Map Quickies

step01

Page 18: Playing with Maps using AngularJS

map.mapTypes.set(

"OSM",

new google.maps.ImageMapType({

getTileUrl: function(coord, zoom) {

return "http://tile.openstreetmap.org/" + zoom + "/" + coord.x + "/" + coord.y + ".png";

},

tileSize: new google.maps.Size(256, 256),

name: "OpenStreetMap",

maxZoom: 18

})

);

Hybrid Map

Page 19: Playing with Maps using AngularJS

Search

var xhr = new XMLHttpRequest();

xhr.open(‘GET’, 'http://nominatim.openstreetmap.org/?q=' + query + '&format=json', true);

xhr.send();

Page 20: Playing with Maps using AngularJS

Search

var searchBox = new google.maps.places.SearchBox(searchInput);

google.maps.event.addListener(searchBox, 'places_changed',callback)

Page 21: Playing with Maps using AngularJS

navigator.geolocation.getCurrentPosition(successCallback, errorCallback, positionOptions)

Geolocation

http://www.w3.org/TR/geolocation-API/

Page 22: Playing with Maps using AngularJS

PositionOptions.enableHighAccuracyPositionOptions.timeoutPositionOptions.maximumAge

Geolocation

function successCallback(position) {

var latitude = position.coords.latitude;

var longitude = position.coords.longitude;

// ...

}

Page 23: Playing with Maps using AngularJS

The Main Course...

Page 24: Playing with Maps using AngularJS

AngularJS Quickie

Page 25: Playing with Maps using AngularJS

Live Code: AngularJS Quickies

step02

Page 26: Playing with Maps using AngularJS

Flow

Page 27: Playing with Maps using AngularJS

DOM

Page 28: Playing with Maps using AngularJS

Nested View<!– In body of index.html --><div ng-controller="MapController">

<div ng-view></div><div class="maps">

<div id="openstreetmap” ng-class="mapfs.OpenStreetMap_class"></div><div id="googlemap” ng-class="mapfs.GoogleMap_class"></div><div id="hybridmap” ng-class="mapfs.HybridMap_class"></div>

</div></div>

<!– In map.html --><div class="actionBox">

<div class="tabs"><ul><li><a ng-class="mapfs.OpenStreetMap_class"

href="#/showOpenStreetMap">OpenStreetMap</a></li></ul></div>…</div>

Page 29: Playing with Maps using AngularJS

var app = angular.module("geoapp", ['ngRoute']);

app.config(function ($routeProvider) { $routeProvider .when('/showOpenStreetMap', { templateUrl: "views/map.html", controller: "OpenStreetMapController"} ) ... .otherwise({redirectTo: '/showOpenStreetMap'}) ;});

Route Definition

Page 30: Playing with Maps using AngularJS

Controllers

Page 31: Playing with Maps using AngularJS

Nested Controllers// Parent Controller.controller('MapController’, function($scope, OpenStreetMap, …){

$scope.mapfs = { OpenStreetMap_class = “” };$scope.showMap = function (mapName) {

if ( mapName === "openstreetmap" ) {$scope.mapfs.OpenStreetMap_class = "active";

}};OpenStreetMap.initMap(“openstreetmap”);… ;

});

// Child Controller.controller(‘OpenStreetMapController’, function ($scope) {

$scope.showMap("openstreetmap");// $scope.mapfs.OpenStreetMap_class is now “active”

});

Page 32: Playing with Maps using AngularJS

Live Code: Nested Controllers

step03

Page 33: Playing with Maps using AngularJS

Providers

Page 34: Playing with Maps using AngularJS

BaseGoogleMap.factory('BaseGoogleMap', function (…) {

var MapObject = function () { … };MapObject.prototype = {

initMap: function (mapId) { … };initSearchBox: function (inputId) { … };

};return MapObject;

});

.factory(‘GoogleMap’, function (BaseGoogleMap) {return new BaseGoogleMap();

});

.factory(‘HybridMap’, function (BaseGoogleMap) {var gmap = new BaseGoogleMap();gmap.initMap = function (mapId) { … };return gmap;

});

Page 35: Playing with Maps using AngularJS

AngularJS Promise.service('OpenStreetMap', function ($http, $q) {

this.search = function (query) {var url = “http://…” + query, d = $q.defer();

$http.get(url,function (response) {

// Update map with data from responsed.resolve(response);

},function (error) { d.reject(error); }

);

return d.promise;};

});

OpenStreetMap.search(“Via del Corso”).then(function (data) {// data is the same response object passed by $d.resolve});// Use Promise in Controller

Page 36: Playing with Maps using AngularJS

Live Code: AngularJS Promise

step04

Page 37: Playing with Maps using AngularJS

https://github.com/marcoslin/sample-geo-angular

Page 38: Playing with Maps using AngularJS
Page 39: Playing with Maps using AngularJS

http://www.flickr.com/photos/vividbreeze/480057824