Skip to content

Instantly share code, notes, and snippets.

@connor11528
Created April 2, 2015 18:31
Show Gist options
  • Save connor11528/a090c329185b94a19de2 to your computer and use it in GitHub Desktop.
Save connor11528/a090c329185b94a19de2 to your computer and use it in GitHub Desktop.
Code to make a leaflet.js map the Angular Way. Comes from a tutorial: http://connorleech.ghost.io/how-to-use-the-angular-leaflet-directive/
var app = angular.module("mymapingapp", [
"leaflet-directive"
]);
app.controller('MainCtrl', ['$scope', function($scope) {
// make map
angular.extend($scope, {
san_fran: {
lat: 37.78,
lng: -122.42,
zoom: 13
},
events: {},
layers: {
baselayers: {
osm: {
name: 'OpenStreetMap',
url: 'https://{s}.tiles.mapbox.com/v3/examples.map-i875mjb7/{z}/{x}/{y}.png',
type: 'xyz'
}
}
},
defaults: {
scrollWheelZoom: false
}
});
// add markers
$scope.markers = new Array();
$scope.$on("leafletDirectiveMap.click", function(event, args){
var leafEvent = args.leafletEvent;
$scope.markers.push({
lat: leafEvent.latlng.lat,
lng: leafEvent.latlng.lng,
draggable: true
});
});
}]);
<!DOCTYPE html>
<html ng-app="mymapingapp">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>My Mapping App</title>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
<style>
#map{
height: 450px;
width: 450px;
}
</style>
</head>
<body ng-controller='MainCtrl'>
<leaflet center="san_fran" markers="markers" event-broadcast="events" defaults="defaults" id='map'></leaflet>
<script src="http://cdn.leafletjs.com/leaflet-0.7.1/leaflet.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js"></script>
<script src="angular-leaflet-directive.js"></script>
<script src='app.js'></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment