Skip to content

Commit

Permalink
Add support for Stylled Markers
Browse files Browse the repository at this point in the history
  • Loading branch information
brianjlandau committed Nov 24, 2010
1 parent 93516a8 commit ca95d69
Show file tree
Hide file tree
Showing 4 changed files with 332 additions and 22 deletions.
44 changes: 40 additions & 4 deletions jquery.jmapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,52 @@
link.attr('href', ("#" + location_data.id));
};

var chooseIconOptions = function(category){
if (settings.category_icon_options){
if ($.isFunction(settings.category_icon_options)){
return settings.category_icon_options(category);
} else {
return settings.category_icon_options[category] || settings.category_icon_options['default'];
}
} else {
return {};
}
};

var createMarker = function(place_elm){
var $place_elm = $(place_elm), place_data, point, marker, $info_window_elm,
info_window;

place_data = $place_elm.metadata(settings.metadata_options);
point = $.jMapping.makeGLatLng(place_data.point);
marker = new google.maps.Marker({
position: point,
map: map
});

if (settings.category_icon_options){
icon_options = chooseIconOptions(place_data.category);
if (typeof icon_options === "string"){
marker = new google.maps.Marker({
icon: icon_options,
position: point,
map: map
});
} else if (icon_options instanceof google.maps.MarkerImage){
marker = new google.maps.Marker({
icon: icon_options,
position: point,
map: map
});
} else {
marker = new StyledMarker({
styleIcon: new StyledIcon(StyledIconTypes.MARKER, icon_options),
position: point,
map: map
});
}
} else {
marker = new google.maps.Marker({
position: point,
map: map
});
}

$info_window_elm = $place_elm.find(settings.info_window_selector);
if ($info_window_elm.length > 0){
Expand Down
18 changes: 9 additions & 9 deletions spec/jmapping_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ Screw.Unit(function(){
});
});

describe("jMapping with no cateogies and 'category_icon_options'", function(){
describe("jMapping with no categories and 'category_icon_options'", function(){
before(function(){
// mock out Marker
google.maps.InfoWindow = Smoke.MockFunction(function(options){}, 'InfoWindow');
Expand All @@ -366,7 +366,7 @@ Screw.Unit(function(){
});
});

mockGMaps();
mockGMaps(true);
mockMarkerManager();
});
after(function(){
Expand All @@ -379,7 +379,7 @@ Screw.Unit(function(){
$('#map').jMapping({
side_bar_selector: 'ul#map-list',
location_selector: 'li.location',
category_icon_options: {'fun': {primaryColor: '#33FFFF'}, 'default': {primaryColor: '#CC0000'}},
category_icon_options: {'fun': {color: '#33FFFF'}, 'default': {color: '#CC0000'}},
metadata_options: {type: 'html5'}
});
expect($('#map').data('jMapping')).to(be_true);
Expand All @@ -398,16 +398,16 @@ Screw.Unit(function(){
});
});

mockGMaps();
mockGMaps(true);
mockMarkerManager();

category_function = mock_function(function(category){
if (category.charAt(0).match(/[a-m]/i)){
return {primaryColor: '#CC0000'};
return {color: '#CC0000'};
} else if (category.charAt(0).match(/[n-z]/i)){
return {primaryColor: '#33FFFF'};
return {color: '#33FFFF'};
} else {
return {primaryColor: '#00FFCC'};
return {color: '#00FFCC'};
}
}, 'category_icon_options_function');
category_function.should_be_invoked().exactly('twice');
Expand Down Expand Up @@ -459,7 +459,7 @@ Screw.Unit(function(){
});
});

describe("setting a function to category_icon_options that returns a GIcon", function(){
describe("setting a function to category_icon_options that returns a MarkerImage", function(){
var category_function;
before(function(){
google.maps.InfoWindow = Smoke.MockFunction(function(options){}, 'InfoWindow');
Expand All @@ -474,7 +474,7 @@ Screw.Unit(function(){
mockMarkerManager();

category_function = mock_function(function(category){
return 'GIcon';
return new google.maps.MarkerImage('/images/map_icons/icon1.jpg');
}, 'category_icon_options_function');
category_function.should_be_invoked().exactly('twice');
});
Expand Down
28 changes: 19 additions & 9 deletions spec/spec_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,24 @@ var google = {
LatLngBounds: function(){},
LatLng: function(){},
Marker: function(){},
InfoWindow: function(){}
InfoWindow: function(){},
MarkerImage: function(){}
}
};
},
StyledIconTypes = {
MARKER: 'MARKER'
},
StyledIcon = function(){},
StyledMarker = function(){};

Screw.Specifications.mockGMaps = function(gmap_mock_setup){
Screw.Specifications.mockGMaps = function(custom_markers){
// mock out GLatLng
google.maps.LatLng = Smoke.MockFunction(function(lat, lng){}, 'LatLng');
google.maps.LatLng.should_be_invoked().at_least('once');

// mock out GMap2
var gmap_mock = Smoke.Mock();
gmap_mock.should_receive('fitBounds').exactly('once');
if ($.isFunction(gmap_mock_setup)){
gmap_mock_setup(gmap_mock);
}
$.extend(google.maps.Map.prototype, gmap_mock);

// mock out Bounds
Expand All @@ -36,8 +39,15 @@ Screw.Specifications.mockGMaps = function(gmap_mock_setup){
$.extend(google.maps.LatLngBounds.prototype, bounds_mock);

// mock out Marker
google.maps.Marker = Smoke.MockFunction(function(point, options){}, 'Marker');
google.maps.Marker.should_be_invoked().exactly('twice');
if (custom_markers){
StyledIcon = Smoke.MockFunction(function(options){}, 'StyledIcon');
StyledIcon.should_be_invoked().exactly('twice');
StyledMarker = Smoke.MockFunction(function(options){}, 'StyledMarker');
StyledMarker.should_be_invoked().exactly('twice');
} else {
google.maps.Marker = Smoke.MockFunction(function(options){}, 'Marker');
google.maps.Marker.should_be_invoked().exactly('twice');
}

google.maps.event = Smoke.Mock();
google.maps.event.should_receive('addListener').at_least('once');
Expand All @@ -60,7 +70,7 @@ Screw.Specifications.mockGMapsUpdate = function(){
$.extend(google.maps.LatLngBounds.prototype, bounds_mock);

// mock out Marker
google.maps.Marker = Smoke.MockFunction(function(point, options){}, 'Marker');
google.maps.Marker = Smoke.MockFunction(function(options){}, 'Marker');
google.maps.Marker.should_be_invoked().exactly(4, 'times');

google.maps.event = Smoke.Mock();
Expand Down
Loading

0 comments on commit ca95d69

Please sign in to comment.