Skip to content

Commit

Permalink
Use lat lng literals instead of converting to google.maps.LatLng.
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanfprice committed Nov 8, 2015
1 parent 9efca98 commit 2298740
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 46 deletions.
6 changes: 0 additions & 6 deletions src/controllers/angulargmMapController.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,19 +251,13 @@

this._newElement = function(type, opts) {
if (type === 'marker') {
if (!(opts.position instanceof google.maps.LatLng)) {
throw 'markerOptions did not contain a position';
}
return new angulargmDefaults.markerConstructor(opts);
} else if (type === 'polyline') {
if (!(opts.path instanceof Array)) {
throw 'polylineOptions did not contain a path';
}
return new angulargmDefaults.polylineConstructor(opts);
} else if (type === 'circle') {
if (!(opts.center instanceof google.maps.LatLng)) {
throw 'circleOptions did not contain a marker position';
}
return new angulargmDefaults.circleConstructor(opts);
}
else {
Expand Down
4 changes: 2 additions & 2 deletions src/directives/gmCircles.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
directive('gmCircles', ['$parse', '$compile', '$timeout', '$log', 'angulargmUtils', 'angulargmShape',
function ($parse, $compile, $timeout, $log, angulargmUtils, angulargmShape) {

var objToLatLng = angulargmUtils.objToLatLng;
var validateLatLng = angulargmUtils.validateLatLng;

function link(scope, element, attrs, controller) {
if (!('gmCircleCenter' in attrs)) {
Expand All @@ -161,7 +161,7 @@

var circleOptions = function (object) {
var latLngObj = scope.gmCircleCenter({ object: object });
var center = objToLatLng(latLngObj);
var center = validateLatLng(latLngObj);
if (center == null) {
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/directives/gmMarkers.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
function($log, $parse, $timeout, angulargmUtils, angulargmShape) {

/** aliases */
var objToLatLng = angulargmUtils.objToLatLng;
var validateLatLng = angulargmUtils.validateLatLng;

function link(scope, element, attrs, controller) {
// check marker attrs
Expand All @@ -172,7 +172,7 @@

var markerOptions = function(object) {
var latLngObj = scope.gmPosition({object: object});
var position = objToLatLng(latLngObj);
var position = validateLatLng(latLngObj);
if (position == null) {
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/directives/gmPolylines.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
directive('gmPolylines', ['$parse', '$compile', '$timeout', '$log', 'angulargmUtils', 'angulargmShape',
function ($parse, $compile, $timeout, $log, angulargmUtils, angulargmShape) {
/** aliases */
var objToLatLng = angulargmUtils.objToLatLng;
var validateLatLng = angulargmUtils.validateLatLng;

function link(scope, element, attrs, controller) {
if (!('gmPath' in attrs)) {
Expand All @@ -164,7 +164,7 @@
var path = [];

angular.forEach(lineLatLngs, function(latlng) {
var position = objToLatLng(latlng);
var position = validateLatLng(latlng);
if (position == null) {
$log.warn('Unable to generate lat/lng from ', latlng);
return;
Expand Down
23 changes: 23 additions & 0 deletions src/services/angulargmUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,28 @@
return null;
}

/**
* @ngdoc function
* @name #validateLatLng
* @methodOf angulargm.service:angulargmUtils
*
* @param {Object} obj of the form { lat: 40, lng: -120 }
* @return {Object} obj or returns null if problems with obj (null,
* NaN, etc.)
*/
function validateLatLng(obj) {
if (obj != null) {
var lat = obj.lat;
var lng = obj.lng;
var ok = !(lat == null || lng == null) && !(isNaN(lat) ||
isNaN(lng));
if (ok) {
return obj;
}
}
return null;
}

/**
* @ngdoc function
* @name #hasNaN
Expand Down Expand Up @@ -160,6 +182,7 @@
boundsEqual: boundsEqual,
latLngToObj: latLngToObj,
objToLatLng: objToLatLng,
validateLatLng: validateLatLng,
hasNaN: hasNaN,
getEventHandlers: getEventHandlers,
assertDefined: assertDefined
Expand Down
2 changes: 1 addition & 1 deletion test/unit/controllers/angulargmMapControllerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('angulargmMapController', function() {

it('constructs the map using the provided map options', function() {
expect(mapCtrl.dragging).toBeFalsy();
expect(mapCtrl.center).toEqual(new google.maps.LatLng(2, 3));
expect(mapCtrl.center.toString()).toEqual(new google.maps.LatLng(2, 3).toString());
expect(mapCtrl.zoom).toEqual(1);
var map = mapCntr.getMap(scope.gmMapId());
expect(mapCtrl.bounds).toEqual(map.getBounds());
Expand Down
2 changes: 1 addition & 1 deletion test/unit/directives/gmMapSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ describe('gmMap', function() {
var center = new google.maps.LatLng(8, 9);
scope.pCenter = center;
scope.$digest();
expect(mapCtrl.center).toEqual(center);
expect(mapCtrl.center.toString()).toEqual(center.toString());
});

it('updates map on scope zoom changed', function() {
Expand Down
60 changes: 30 additions & 30 deletions test/unit/directives/gmMarkersSpec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe('gmMarkers', function() {
var elm, scope, markersScopeId, mapCtrl;
var objToLatLng;
var latLngToObj;
var $timeout;

beforeEach(function() {
Expand All @@ -11,8 +11,8 @@ describe('gmMarkers', function() {
// set up scopes
scope = $rootScope.$new();
scope.people = [
{name: '0', lat: 1, lng: 2, id: 0},
{name: '3', lat: 4, lng: 5, id: 3}
{name: '0', id: 0, location: {lat: 1, lng: 2}},
{name: '3', id: 3, location: {lat: 4, lng: 5}}
];
scope.getOpts = function(person) {
return {
Expand All @@ -23,14 +23,14 @@ describe('gmMarkers', function() {
scope.mapId = 'test';

$timeout = _$timeout_;
objToLatLng = angulargmUtils.objToLatLng;
latLngToObj = angulargmUtils.latLngToObj

// compile angulargmMarkers directive
elm = angular.element('<gm-map gm-map-id="mapId" gm-center="center" gm-zoom="zoom" gm-bounds="bounds">' +
'<gm-markers ' +
'gm-objects="people"' +
'gm-id="object.id"' +
'gm-position="{lat:object.lat,lng:object.lng}"' +
'gm-position="object.location"' +
'gm-marker-options="getOpts(object)"' +
'gm-events="markerEvents"' +
'gm-on-click="selected = {person: object, marker: marker}"' +
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('gmMarkers', function() {
elm = angular.element('<gm-map gm-map-id="mapId" gm-center="center" gm-zoom="zoom" gm-bounds="bounds">' +
'<gm-markers ' +
'gm-id="{id:object.id}"' +
'gm-position="{lat:object.lat,lng:object.lng}"' +
'gm-position="object.location"' +
'gm-marker-options="getOpts(object)"' +
'gm-on-click="selected = {person: object, marker: marker}"' +
'gm-on-mouseover="mouseovered = {person: object, marker: marker}">' +
Expand All @@ -75,7 +75,7 @@ describe('gmMarkers', function() {
elm = angular.element('<gm-map gm-map-id="mapId" gm-center="center" gm-zoom="zoom" gm-bounds="bounds">' +
'<gm-markers ' +
'gm-objects="people"' +
'gm-position="{lat:object.lat,lng:object.lng}"' +
'gm-position="object.location"' +
'gm-marker-options="getOpts(object)"' +
'gm-on-click="selected = {person: object, marker: marker}"' +
'gm-on-mouseover="mouseovered = {person: object, marker: marker}">' +
Expand Down Expand Up @@ -108,8 +108,8 @@ describe('gmMarkers', function() {
describe('objects', function() {

it('initializes markers with objects', function() {
var position1 = objToLatLng(scope.people[0]);
var position2 = objToLatLng(scope.people[1]);
var position1 = scope.people[0].location;
var position2 = scope.people[1].location;
expect(mapCtrl.addElement).toHaveBeenCalledWith('marker', markersScopeId,
jasmine.any(String), {key: 'value', title: jasmine.any(String), position: position1});
expect(mapCtrl.addElement).toHaveBeenCalledWith('marker', markersScopeId,
Expand All @@ -118,8 +118,8 @@ describe('gmMarkers', function() {


it('updates markers with new objects', function() {
scope.people.push({name: '6', lat: 7, lng: 8, id: 6});
var position = objToLatLng(scope.people[2]);
scope.people.push({name: '6', id: 6, location: {lat: 7, lng: 8}});
var position = scope.people[2].location;
scope.$digest();
expect(mapCtrl.addElement).toHaveBeenCalledWith('marker', markersScopeId,
jasmine.any(String), {key: 'value', title: jasmine.any(String), position: position});
Expand All @@ -131,7 +131,7 @@ describe('gmMarkers', function() {
var length = scope.people.length;
scope.people = [];
for (var i = 0; i < length; i++) {
scope.people.push({name: 'new' + i, lat: i, lng: i, id: (i+10)});
scope.people.push({name: 'new' + i, id: (i+10), location: {lat: i, lng: i}});
}
scope.$digest();
expect(mapCtrl.removeElement.calls.length).toEqual(length);
Expand All @@ -142,24 +142,24 @@ describe('gmMarkers', function() {
it('updates markers with removed objects', function() {
var person = scope.people.pop();
scope.$digest();
var position = objToLatLng(person);
var position = person.location;
expect(mapCtrl.removeElement).toHaveBeenCalledWith('marker', markersScopeId, '3');
});


it('does not add markers with duplicate ids', function() {
var origLength = scope.people.length;
scope.people.push({name: '0', lat: 2, lng: 3, id: 0});
scope.people.push({name: '0', id: 0, location: {lat: 2, lng: 3}});
scope.$digest();
expect(mapCtrl.addElement.callCount).toEqual(origLength);
});


it('updates markers with changed objects when update triggered', function() {
var person = scope.people[0]
person.lat = person.lat + 5;
person.lng = person.lng + 5;
var newPosition = objToLatLng(person);
person.location.lat = person.location.lat + 5;
person.location.lng = person.location.lng + 5;
var newPosition = person.location;
scope.$broadcast('gmMarkersUpdate', 'people');
expect(mapCtrl.updateElement).toHaveBeenCalledWith('marker', markersScopeId,
jasmine.any(String), {key: 'value', title: jasmine.any(String), position: newPosition});
Expand All @@ -186,7 +186,7 @@ describe('gmMarkers', function() {

it('triggers events', function() {
var person = scope.people[0];
var position = objToLatLng(person);
var position = person.location;
var id = person.name
scope.markerEvents = [{
event: 'click',
Expand All @@ -197,15 +197,15 @@ describe('gmMarkers', function() {
$timeout.flush();
var marker = mapCtrl.trigger.mostRecentCall.args[0];
var event = mapCtrl.trigger.mostRecentCall.args[1];
expect(marker.getPosition()).toEqual(position);
expect(latLngToObj(marker.getPosition())).toEqual(position);
expect(event).toEqual('click');
});


it('triggers events on multiple markers', function() {

var position0 = objToLatLng(scope.people[0]);
var position1 = objToLatLng(scope.people[1]);
var position0 = scope.people[0].location;
var position1 = scope.people[1].location;
var id0 = scope.people[0].name
var id1 = scope.people[1].name
scope.markerEvents = [{
Expand All @@ -216,13 +216,13 @@ describe('gmMarkers', function() {
$timeout.flush();
var marker0 = mapCtrl.trigger.calls[0].args[0];
var marker1 = mapCtrl.trigger.calls[1].args[0];
expect(marker0.getPosition()).toEqual(position0);
expect(marker1.getPosition()).toEqual(position1);
expect(latLngToObj(marker0.getPosition())).toEqual(position0);
expect(latLngToObj(marker1.getPosition())).toEqual(position1);
});


it('triggers multiple events on markers', function() {
var position = objToLatLng(scope.people[0]);
var position = scope.people[0].location;
var id = scope.people[0].name
scope.markerEvents = [
{
Expand Down Expand Up @@ -270,8 +270,8 @@ describe('gmMarkers', function() {


it('listens for marker redraw event', function() {
var position1 = objToLatLng(scope.people[0]);
var position2 = objToLatLng(scope.people[1]);
var position1 = scope.people[0].location;
var position2 = scope.people[1].location;
scope.getOpts = function(person) {
return {
key: 'differentValue',
Expand All @@ -288,8 +288,8 @@ describe('gmMarkers', function() {


it('listens to marker redraw event when no objects specified', function() {
var position1 = objToLatLng(scope.people[0]);
var position2 = objToLatLng(scope.people[1]);
var position1 = scope.people[0].location;
var position2 = scope.people[1].location;
scope.getOpts = function(person) {
return {
key: 'differentValue',
Expand Down Expand Up @@ -322,8 +322,8 @@ describe('gmMarkers', function() {


it('listens for marker update event', function() {
var position1 = objToLatLng(scope.people[0]);
var position2 = objToLatLng(scope.people[1]);
var position1 = scope.people[0].location;
var position2 = scope.people[1].location;
scope.getOpts = function(person) {
return {
key: 'differentValue',
Expand Down
4 changes: 2 additions & 2 deletions test/unit/services/angulargmUtilsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ describe('angulargmUtils', function() {
describe('objToLatLng', function() {

it('converts an object to a latLng', function() {
var latLng = objToLatLng(latLngObj);
expect(latLng).toEqual(latLngG);
var latLng = latLngToObj(objToLatLng(latLngObj));
expect(latLng).toEqual(latLngObj);
});

it('returns null on NaN', function() {
Expand Down

0 comments on commit 2298740

Please sign in to comment.