Skip to content

Commit

Permalink
added randomize option
Browse files Browse the repository at this point in the history
  • Loading branch information
apneadiving committed Mar 19, 2011
1 parent e136a39 commit fb28cfa
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 11 deletions.
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) 2010 Benjamin Roth

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
4 changes: 2 additions & 2 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ Done!

* Marker clusterer customization

* {Integrate Markers with Label} [http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerwithlabel/1.1.2/]
* Integrate Markers with Label: http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerwithlabel/1.1.2/

* {Other options from here} [http://code.google.com/p/google-maps-utility-library-v3/wiki/Libraries] ?
* Other options from here: http://code.google.com/p/google-maps-utility-library-v3/wiki/Libraries ?

Feel free ton contact us, you have your say.

Expand Down
Binary file added pkg/gmaps4rails-0.6.1.gem
Binary file not shown.
40 changes: 35 additions & 5 deletions public/javascripts/gmaps4rails.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ var Gmaps4Rails = {
center_latitude : 0,
center_longitude : 0,
zoom : 1,
maxZoom: null,
minZoom: null,
auto_adjust : false //adjust the map to the markers if set to true
},

Expand All @@ -22,7 +24,9 @@ var Gmaps4Rails = {
//clustering config
do_clustering: true, //do clustering if set to true
clusterer_gridSize: 50, //the more the quicker but the less precise
clusterer_maxZoom: 10 //removes clusterer at this zoom level
clusterer_maxZoom: 5, //removes clusterer at this zoom level
randomize: true, //Google maps can't display two markers which have the same coordinates. This randomizer enables to prevent this situation to happen.
max_random_distance: 100 //in meters. Each marker coordinate could be altered by this distance in a random direction
},

//Stored variables
Expand Down Expand Up @@ -80,6 +84,8 @@ var Gmaps4Rails = {
//initializes the map
initialize: function(){
this.map = new google.maps.Map(document.getElementById(this.map_options.id), {
maxZoom: this.map_options.maxZoom,
minZoom: this.map_options.minZoom,
zoom: this.map_options.zoom,
center: new google.maps.LatLng(this.map_options.center_latitude, this.map_options.center_longitude),
mapTypeId: google.maps.MapTypeId[this.map_options.type]
Expand Down Expand Up @@ -340,8 +346,18 @@ var Gmaps4Rails = {
var marker_width = this.exists(this.markers[i].width) ? this.markers[i].width : this.markers_conf.width;
var marker_height = this.exists(this.markers[i].height) ? this.markers[i].height : this.markers_conf.length;
var marker_title = this.exists(this.markers[i].title) ? this.markers[i].title : null;

var myLatLng = new google.maps.LatLng(this.markers[i].latitude, this.markers[i].longitude);
var Lat = this.markers[i].latitude;
var Lng = this.markers[i].longitude;

//alter coordinates if randomize is true
if ( this.markers_conf.randomize)
{
var LatLng = this.randomize(Lat, Lng);
//retrieve coordinates from the array
Lat = LatLng[0]; Lng = LatLng[1];
}

var myLatLng = new google.maps.LatLng(Lat, Lng);
this.extend_bounds(myLatLng);

// Marker sizes are expressed as a Size of X,Y
Expand All @@ -354,7 +370,7 @@ var Gmaps4Rails = {
}
//save object for later use, basically, to get back the text to display when clicking it
this.markers[i].marker_object = ThisMarker;
//save the marker again in a list for the clusterer
//save the marker in a list
marker_objects.push(ThisMarker);
//add click listener
google.maps.event.addListener(Gmaps4Rails.markers[i].marker_object, 'click', function() { if (Gmaps4Rails.info_window!=null) {Gmaps4Rails.info_window.close();}; Gmaps4Rails.getInfoWindow(this);});
Expand Down Expand Up @@ -399,7 +415,21 @@ var Gmaps4Rails = {
//basic function to check existence of a variable
exists: function(var_name) {
return var_name != "" && typeof var_name !== "undefined"
}
},

//randomize
randomize: function(Lat0, Lng0) {
//distance in meters between 0 and max_random_distance (positive or negative)
var dx = this.markers_conf.max_random_distance * this.random();
var dy = this.markers_conf.max_random_distance * this.random();
var Lat = parseFloat(Lat0) + (180/Math.PI)*(dy/6378137);
var Lng = parseFloat(Lng0) + ( 90/Math.PI)*(dx/6378137)/Math.cos(Lat0);
return [Lat, Lng];
},

//retrives a value between -1 and 1
random: function() { return ( Math.random() * 2 -1); }

};

//marker_clusterer styles
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ../../
specs:
gmaps4rails (0.6.0)
gmaps4rails (0.6.1)
crack

GEM
Expand Down
5 changes: 2 additions & 3 deletions test/dummy/app/views/users/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ var builder2 = [{"description": "",
} ];
</script>
<% @options = {
"map_options" => { "type" => "SATELLITE", "center_longitude" => 180, "zoom" => 3},
"map_options" => { "type" => "SATELLITE", "center_longitude" => 180, "zoom" => 3, "auto_adjust" => true},
"markers" => { "data" => '[{ "description": "", "title": "", "longitude": "5.9311119", "latitude": "43.1251606", "picture": "", "width": "", "height": "" } ,{ "description": "", "title": "", "longitude": "2.3509871", "latitude": "48.8566667", "picture": "", "width": "", "height": "" } ]',
"options" => { "auto_adjust" => true }
},
},
"polylines" => { "data" => '[[
{"longitude": -122.214897, "latitude": 37.772323},
{"longitude": -157.821856, "latitude": 21.291982},
Expand Down

0 comments on commit fb28cfa

Please sign in to comment.