Skip to content

Instantly share code, notes, and snippets.

@mustmodify
Last active December 17, 2015 02:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mustmodify/5533979 to your computer and use it in GitHub Desktop.
Save mustmodify/5533979 to your computer and use it in GitHub Desktop.
This gist is my attempt to get MapQuest to work with AngularJS templates.
// NOTE: ng-app="plunker" + AngularUI module dependency
var app = angular.module('map-app', []);
app.controller('MapCtrl', function($scope) {
$scope.address = function()
{
out = "";
if( $scope.location.street1 != undefined )
{
out += $scope.location.street1;
}
if( $scope.location.street2 != undefined )
{
out += " " + $scope.location.street2;
}
if( $scope.location.city != undefined, $scope.location.state != undefined )
{
out += " " + $scope.location.city + ", " + $scope.location.state;
}
if( $scope.location.zipcode != undefined && $scope.location.zipcode.length >= 5 )
{
out += " " + $scope.location.zipcode;
}
out += " USA";
return(out);
};
$scope.complete_zipcode = function()
{
return(($scope.location.zipcode != undefined) && ($scope.location.zipcode.length >= 5))
};
$scope.update_map_location = function(){
if($scope.complete_zipcode())
{
MQA.withModule('nominatim', function()
{
window.map.removeAllShapes();
console.log("Looking for address" + $scope.address());
map.nominatimSearchAndAddLocation($scope.address());
});
}
};
});
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.draw_map = function()
{
var options={
elt:document.getElementById('map'), /*ID of element on the page where you want the map added*/
zoom:13, /*initial zoom level of the map*/
latLng:{lat:40.735383, lng:-73.984655}, /*center of map in latitude/longitude */
mtype:'osm', /*map type (osm)*/
bestFitMargin:0, /*margin offset from the map viewport when applying a bestfit on shapes*/
zoomOnDoubleClick:true /*zoom in when double-clicking on map*/
};
window.map = new MQA.TileMap(options);
};
$scope.$on('$routeChangeSuccess', function(next, current, $location) {
if( $location != undefined )
{
$scope.draw_map();
}
});
});
app.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/map', {templateUrl: 'map.html'}).
otherwise({redirectTo: '/map'});
}]);
<!doctype html>
<html ng-app="map-app" >
<head>
<meta charset="utf-8">
<title>AngularJS and MapQuest Plunker</title>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/themes/base/jquery-ui.css">
<link href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" rel="stylesheet">
<link rel="stylesheet" href="style.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<script src="https://raw.github.com/twitter/bootstrap/master/docs/assets/js/bootstrap.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular-resource.min.js"></script>
<script src="https://raw.github.com/angular-ui/angular-ui/master/build/angular-ui.js"></script>
<script src="app.js"></script>
<script src="http://open.mapquestapi.com/sdk/js/v7.0.s/mqa.toolkit.js?key=Fmjtd%7Cluub2d6tn1%2C22%3Do5-9u22uu "></script>
</head>
<body>
<div ng-controller="MainCtrl">
Hello {{name}}!
</div>
<div ng-view></div>
</body>
</html>
<div class="row" ng-controller="MapCtrl">
<div class="span6">
<form class="form form-horizontal ng-pristine ng-valid" id="new_customer_form">
<div class="control-group zipcode">
<label class="control-label" for="customer_zipcode">
Zipcode
</label>
<div class="controls">
<input class="addressable zipcode ng-pristine ng-valid" id="customer_zipcode" ng-model="location.zipcode" type="text" ng-change="update_map_location()">
</div>
</div>
<div class="control-group street1">
<label class="control-label" for="customer_street1">
Street
</label>
<div class="controls">
<input class="addressable street ng-pristine ng-valid" id="customer_street1" ng-model="location.street1" type="text" ng-change="update_map_location()">
</div>
</div>
<div class="control-group street2">
<div class="controls">
<input id="customer_street2" ng-model="location.street2" type="text" class="ng-pristine ng-valid" ng-change="update_map_location()">
</div>
</div>
<div class="control-group city">
<label class="control-label" for="customer_city">
City
</label>
<div class="controls">
<input class="addressable city ng-pristine ng-valid" id="customer_city" ng-model="location.city" type="text" ng-change="update_map_location()">
</div>
</div>
<div class="control-group state">
<label class="control-label" for="customer_state">
State
</label>
<div class="controls">
<input class="addressable state ng-pristine ng-valid" id="customer_state" ng-model="location.state" type="text" ng-change="update_map_location()">
</div>
</div>
</form>
</div>
<div class="map-container span6">
Here is the map
<div id="map" style="width:300px; height:150px;" ng-model="map"></div>
</div>
</div>
/* Put your css in here */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment