Skip to content

Commit 418d6e9

Browse files
committed
Created a MapViewDelegate and MapViewProxy for better map control
1 parent e3127af commit 418d6e9

File tree

5 files changed

+300
-79
lines changed

5 files changed

+300
-79
lines changed

Diff for: core.js

+9-52
Original file line numberDiff line numberDiff line change
@@ -22,64 +22,21 @@ MapKit = SC.Object.create(
2222
API_KEY: null,
2323
PINS: null,
2424
store: null,
25+
MAPS: [],
2526

2627
// ..........................................................
2728
// NS FUNCTIONS
2829
//
29-
/*
30-
Adds a pin to the map using an address string
31-
@param [string] Herndon, VA 20171
32-
*/
33-
addPinForAddress: function(address) {
34-
this._getPointForAdderessIfPossible(address);
35-
},
36-
37-
/*
38-
Adds a pin to the map using an object containing latitude and longitude
39-
@param [hash] { lat: 33.79, lng: -77.89 }
40-
*/
41-
addPinForLatAndLng: function(point) {
42-
if (point && point.lat && point.lng) {
43-
var pin = this.get('store').createRecord(MapKit.Pin,{latitude: point.lat, longitude: point.lng });
44-
MapKit.pinsController.selectObject(pin);
45-
return pin;
46-
}
47-
},
48-
49-
/*
50-
Moves the center of the map to the pin.
51-
Uses the selction from MapKit.pinController
52-
@param [mapView] the current mapView
53-
*/
54-
moveMapToPin: function(map, pin) {
55-
SC.Logger.log("Moving to pin");
56-
if (map && pin) {
57-
map.setCenter(pin);
58-
map.setZoom(7);
30+
proxyForMap: function(mapNameOrGUID) {
31+
var proxy, maps = this.get('MAPS');
32+
if (mapNameOrGUID && maps && maps.get('length') > 1) {
33+
var obj = maps.findProperty('key', mapNameOrGUID);
34+
return obj.get('proxy');
35+
} else if (maps.get('length') === 1) {
36+
return maps.getPath('firstObject.proxy');
5937
} else {
60-
SC.Logger.log("You Must Provide a map view");
38+
SC.Logger.warn("Proxy for %@ could not be found...");
6139
}
62-
},
63-
// ..........................................................
64-
// NS PRIVATE FUNCTIONS
65-
//
66-
_getPointForAdderessIfPossible: function(address){
67-
var that = this, ns = this.MAPS_NAMESPACE, lat, lng;
68-
var geocoder = new ns.ClientGeocoder();
69-
geocoder.getLatLng(address, function(point){
70-
if (point) {
71-
lat = point.lat(); lng = point.lng();
72-
SC.run(function(){
73-
var pin = that.get('store').createRecord(MapKit.Pin,{
74-
latitude: lat, longitude: lng, address: address, name: address
75-
});
76-
MapKit.pinsController.selectObject(pin);
77-
});
78-
} else {
79-
alert(address + " could not be found.");
80-
}
81-
});
82-
8340
}
8441

8542
});

Diff for: mixins/map_view_delegate.js

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
//============================================================================
2+
// MapKit.MapViewDelegate
3+
//============================================================================
4+
/*globals MapKit*/
5+
6+
/**
7+
8+
@namespace
9+
10+
A Map View Delegate is consulted by a MapKit.MapView to make the API calls
11+
to google maps. It allows you override the default implementation of
12+
these API calls for finer control.
13+
14+
@author Josh Holt
15+
@version 0.1.0
16+
@since 0.1.0
17+
18+
*/
19+
20+
MapKit.MapViewDelegate = {
21+
22+
isMapViewDelegate: YES,
23+
24+
/*
25+
Clears all overlays on the map
26+
@param {MapKit.MapView} view the MapView
27+
*/
28+
mapViewClearOverLays: function(view) {
29+
var map = view.get('_googleMap');
30+
if (map) {
31+
map.clearOverlays();
32+
view.set('currentPins',[]);
33+
}
34+
},
35+
36+
/*
37+
Sets the center of the map
38+
@param {MapKit.MapView} view the MapView
39+
@param {MapKit.Pin} a pin/location on the map
40+
*/
41+
mapViewSetCenter: function(view, pin) {
42+
var map = view.get('_googleMap');
43+
if (map) {
44+
map.setCenter(pin.get('googleLatLng'));
45+
}
46+
},
47+
48+
/*
49+
Adds a pin to the map
50+
@param {MapKit.MapView} view the MapView
51+
@param {MapKit.Pin} a pin/location on the map
52+
*/
53+
mapViewAddPin: function (view, pin) {
54+
var marker, map = view.get('_googleMap');
55+
var ns = MapKit.MAPS_NAMESPACE, icon = new ns.Icon(ns.DEFAULT_ICON);
56+
57+
icon.image = pin.get('iconURLS').icon;
58+
icon.iconSize = new ns.Size(32,32);
59+
icon.iconAnchor = new ns.Point(16,32);
60+
icon.shadow = pin.get('iconURLS').shadow;
61+
icon.shadowSize = new ns.Size(59,32);
62+
63+
marker = new ns.Marker(pin.get('googleLatLng'),
64+
{'icon':icon, 'clickable':false, 'draggable':false});
65+
pin.set('marker',marker);
66+
map.addOverlay(marker);
67+
},
68+
69+
/*
70+
Removes a pin from the map
71+
@param {MapKit.MapView} view the MapView
72+
@param {MapKit.Pin} a pin/location on the map
73+
*/
74+
mapViewRemovePin: function (view, pin) {
75+
var map = view.get('_googleMap');
76+
if (map) {
77+
map.removeOverlay(pin.get('marker'));
78+
}
79+
},
80+
81+
/*
82+
Sets the zoomLevel for the map
83+
@param {MapKit.MapView} view the MapView
84+
@param {Number} a number between 1 -- 10
85+
*/
86+
mapViewSetZoom: function (view, zoomLevel) {
87+
var map = view.get('_googleMap');
88+
if (map) {
89+
map.setZoom(zoomLevel);
90+
}
91+
},
92+
93+
/*
94+
Boolean to let you know if the map is ready
95+
@param {MapKit.MapView} view the MapView
96+
*/
97+
mapViewIsMapReady: function(view) {
98+
return view.get('_isMapReady');
99+
},
100+
101+
/*
102+
Moves the center of the map to the specified pin
103+
@param {MapKit.MapView} view the MapView
104+
@param {MapKit.Pin} a pin/location on the map
105+
*/
106+
mapViewMoveMapToPin: function(view, pin) {
107+
var map = view.get('_googleMap');
108+
if (map && pin) {
109+
map.setCenter(pin.get('googleLatLng'));
110+
map.setZoom(7);
111+
}
112+
}
113+
114+
};

Diff for: models/pin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
MapKit.Pin = SC.Record.extend({
18-
markerIcon: SC.Record.attr(String, {defaultValue: 'red-pushpin'}),
18+
markerIcon: SC.Record.attr(String, {defaultValue: 'blue-dot'}),
1919
name: SC.Record.attr(String, "(NO NAME)"),
2020
address: SC.Record.attr(String, {defaultValue: 'Herndon, VA'}),
2121
latitude: SC.Record.attr(String),

Diff for: proxies/map_view_proxy.js

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
//============================================================================
2+
// MapKit.MapViewDelegate
3+
//============================================================================
4+
/*globals MapKit*/
5+
6+
sc_require('mixins/map_view_delegate');
7+
/**
8+
9+
10+
A Map View proxy gives you the API for a mapview and allows you to
11+
call those API funcitons without having to pass the view refrence
12+
around.
13+
14+
@author Josh Holt
15+
@version 0.1.0
16+
@since 0.1.0
17+
18+
*/
19+
20+
MapKit.MapViewProxy = SC.Object.extend(MapKit.MapViewDelegate,{
21+
22+
isMapViewDelegate: NO,
23+
mapView: null,
24+
25+
addPin: function(pin) {
26+
var map = this.get('mapView');
27+
if (pin && map) {
28+
this.mapViewAddPin(map, pin);
29+
}
30+
},
31+
32+
removePin: function(pin) {
33+
var map = this.get('mapView');
34+
if (pin && map) {
35+
this.mapViewRemovePin(map, pin);
36+
}
37+
},
38+
39+
setCenter: function(pin) {
40+
var map = this.get('mapView');
41+
if (pin && map) {
42+
this.mapViewSetCenter(map, pin);
43+
}
44+
},
45+
46+
setZoom: function(zoomLevel) {
47+
var map = this.get('mapView');
48+
if (zoomLevel && map) {
49+
this.mapViewSetZoom(map, zoomLevel);
50+
}
51+
},
52+
53+
clearOverlays: function() {
54+
var map = this.get('mapView');
55+
if (map) {
56+
this.mapViewClearOverLays(map);
57+
}
58+
},
59+
60+
moveMapToPin: function(pin) {
61+
var map = this.get('mapView');
62+
if (pin && map) {
63+
this.mapViewMoveMapToPin(map, pin);
64+
}
65+
},
66+
67+
isMapReady: function() {
68+
var map = this.get('mapView');
69+
if (map) {
70+
return this.mapViewIsMapReady(map);
71+
}
72+
},
73+
74+
/*
75+
Adds a pin to the map using an address string
76+
@param [string] Herndon, VA 20171
77+
*/
78+
addPinForAddress: function(address) {
79+
this._getPointForAdderessIfPossible(address);
80+
},
81+
82+
/*
83+
Adds a pin to the map using an object containing latitude and longitude
84+
@param [hash] { lat: 33.79, lng: -77.89 }
85+
*/
86+
addPinForLatAndLng: function(point) {
87+
if (point && point.lat && point.lng) {
88+
var pin = MapKit.get('store').createRecord(MapKit.Pin,
89+
{latitude: point.lat, longitude: point.lng });
90+
MapKit.pinsController.selectObject(pin);
91+
return pin;
92+
}
93+
},
94+
95+
// ..........................................................
96+
// NS PRIVATE FUNCTIONS
97+
//
98+
_getPointForAdderessIfPossible: function(address){
99+
var that = this, ns = MapKit.MAPS_NAMESPACE, lat, lng;
100+
var geocoder = new ns.ClientGeocoder();
101+
geocoder.getLatLng(address, function(point){
102+
if (point) {
103+
lat = point.lat(); lng = point.lng();
104+
SC.run(function(){
105+
var pin = MapKit.get('store').createRecord(MapKit.Pin,{
106+
latitude: lat, longitude: lng, address: address, name: address
107+
});
108+
MapKit.pinsController.selectObject(pin);
109+
});
110+
} else {
111+
alert(address + " could not be found.");
112+
}
113+
});
114+
115+
}
116+
117+
});

0 commit comments

Comments
 (0)