There isn’t a whole lot of info out there about how to use Mapstraction, especially the v2 API, so here’s one thing I’ve had to pick up along the way.
I spent a little time hacking together a geocoder module that uses the Google Maps v3 API, but was having to track down why it wasn’t loading properly. In the mxn.js file, there’s a note about how to activate various modules, but this isn’t clear in the Mapstraction v2 Sandbox on appspot. Here’s the comment:
1
2
3
4
5
6
7
8
9
10
11
12
|
// Auto-load scripts
//
// specify which map providers to load by using
// <script src="mxn.js?(provider1,provider2,[module1,module2])" ...
// in your HTML
//
// for each provider mxn.provider.module.js and mxn.module.js will be loaded
// module 'core' is always loaded
//
// NOTE: if you call without providers
// <script src="mxn.js" ...
// no scripts will be loaded at all and it is then up to you to load the scripts independently
|
Right, so to use the geocoding capabilities you need to do something like this in your HTML file:
1 |
<script src="mxn.js?(googlev3,[geocoder])"></script>
|
Then you just do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
// Initialize Map Layer.
var m10n;
m10n = new mxn.Mapstraction('map_canvas', 'googlev3');
m10n.enableScrollWheelZoom();
m10n.setCenterAndZoom(new mxn.LatLonPoint(52.52, 13.41404), 8);
m10n.addControls({
zoom: 'small'
});
var geocoder;
var address;
$('#goGeocode').click(function(event) {
geocoder = new mxn.Geocoder('googlev3', function(result) {
debug.info(result);
m10n.setCenterAndZoom(result.point, 8);
});
address = {};
address.street = "1600 Pennsylvania Ave.";
address.locality = "Washington";
address.region = "DC";
address.country = "US";
geocoder.geocode(address);
event.preventDefault();
});
|
And here’s the Geocoder plugin, developed with help from Google Chrome’s awesome Javascript Debugger (a definite notch above Venkman, and snappier):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
/*
Copyright (c) 2010 Max Vilimpoc
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of the Mapstraction nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
mxn.register('googlev3', {
Geocoder: {
init: function(element, api) {
// During debugging, it looks like the apply is getting passed an array with some
// transposed elements (breaking API change? mapquest doesn't work either.)
if ('googlev3' === element) {
api = element;
}
this.geocoders[api] = new google.maps.Geocoder();
},
geocode: function(address){
var me = this;
// If there was no address (or an empty) string property, then just make a blank one up.
if (!address.hasOwnProperty('address') || address.address === null || address.address === '') {
address.address = [ address.street, address.locality, address.region, address.country ].join(', ');
}
// Call Google.
this.geocoders[this.api].geocode({ 'address' : 'Detroit, MI' }, function(resultArray, resultStatus) {
var response = { results: resultArray, status: resultStatus };
me.geocode_callback(response);
});
},
geocode_callback: function(response) {
var return_location = {};
if (response) {
if (!response.hasOwnProperty('status') || google.maps.Geocoder.OK != response.status) {
this.error_callback(response);
}
return_location.street = '';
return_location.locality = '';
return_location.region = '';
return_location.country = '';
//
var closest = response.results[0];
return_location.point = new mxn.LatLonPoint(closest.geometry.location.lat(),
closest.geometry.location.lng());
this.callback(return_location);
}
}
}
});
|