Skip to main content

Fun with Google Maps api and Marker clusters :)

Hello Everyone, has been a long time since i wrote my last blog :(.
So i am back with some helpful tips that might help someone.So lets quickly jump to different topics to customise google maps and how to use it in the way we want.Let start -


  1. Customise Google Places Autocomplete, so that it can only provide place nearby or relevant instead of global suggestions.Its simple, just use strictBounds
    var options ={
        ///all options in English language: 'en-US',
        ///IMP -> LIFE SAVER :)
        strictBounds: true,
        //limit only to searching of places in INDIA ONLY
        componentRestrictions: { country: "IN" }
    }


And then pass the options, along with the input field to the Autocomplete api.

var input = document.getElementById('find_locations');
var autocomplete = new google.maps.places.Autocomplete(input, options);

      2. Moreover, what if you want to check if the location selected from the autocomplete are within a state or a region.The below code bounds the users search based on bounds object and then return true or false, if its within the bound or outside of it.

To start with, firstly you need to define a bounding box or bounds, which is a box containing your location/state/region/area...any area on earth :).You must provide NorthEast and SouthWest coordinates to create a bound object.Now, you can try the json responses on the google maps documentation here and pass on the required location /state name to get NE/SW coordinates.

Then, once you have your bounding box defined, you can use the contains method, which returns true/false if the selected address is not within the box.Here's the code -

//this is the NE/SW coodinates for Maharashtra, one of the state in India       
var southWest = new google.maps.LatLng(15.6024121,72.659363);  
var northEast = new google.maps.LatLng(22.028441,80.890924); 
///HERE, i define my bounding box using NE/SW coordinates
var bounds = new google.maps.LatLngBounds(southWest,northEast);      
//i will bind the bounds with my input field      
var input = document.getElementById('geocomplete_and_load');
//obviously, i need the options too        
var options = {          
    bounds: bounds,
    componentRestrictions: {country: 'in'},
    types: ["geocode", "establishment"]};
}
 /// here , now i have binded all the settings with my input field
 var autocomplete = new google.maps.places.Autocomplete(input, options);          
  //LIFE SAVER :), check if location is within the state or not
  if (bounds.contains(place.geometry.location)) {
     console.log("========Hurray, the selected place lies within Maharashtra");
  } else {
     console.log("========Oops, the state is outside of Maharashtra");
  }


       3. Using markerCluster.js - google map library, along with rails(yeah, i know we all love it).

Now clustering of markers(where all the markers are grouped into one cluster/marker)is something like the image below - .
This awwwwsome javascript will group the nearby latlng and then combine it into one.You can read more about it on the google api page


So its obvious, you need to pass an array of markers(which requires latlng too)to the cluster api.Now i have implemented this using rails json and then i pass the json to the api.

The below code is self-explanatory with comments added wherever necessary.

//Firstly, get the json.- either from controller/ model(your choice 👍)
##my model - using Geocoder gem enabled to store latitude/longitude.
       @products = Product.all_available_products
       ##we need the latlng to create dynamic markers and then pass those markers to the api.
       @products_json = @products.map do |h|
          { :id => h.id, :latitude=>h.address.latitude, :longitude=>h.address.longitude}
       end

//Then, pass the json structured object to the view, so in the view  its like this -
      <%= javascript_tag do%>
           window.show_map_nearbys= <%=raw @products_json.to_json %>;
      <%end%>     
//Most important part - in js, play around with the json values by iterating them and then using the marker cluster.

            //array to store infowindows(because you need to show something when you click on the marker), lat, lng
            var locations = [];
            //array to store markers
            var gmarkers = [];
            //just a variable to be used to define marker dynamically before pushing it to gmarkers above
            var markers =[];
            
            ////FIRSTLY, loop around the json and create infowindows - to show on click of the markers, you may just add a simple title, thats it.
            $.each(show_map_nearbys, function(index) {
             
             
            var info = '<div class="infowindowContainer pad0" style="border:none;overflow:hidden;" >' +
                                '<div class="thumbnail pad0">'+
                                '<p class="viewed_count_show_page_infowindow">'+
                                '<span class="fa fa-eye marR5"></span>'+
                                'Viewed '+show_map_nearbys[index]['page_views_count']+ ' time(s)' +
                                '</p>'+                              
                                    //add more dynamic details                
                                '</div>';        
                locations.push({['lat']: show_map_nearbys[index]['latitude'], ['lng']: show_map_nearbys[index]['longitude'], ['info']: info});                
            
            });//each loop ends    
                 
            //create google map infowindow object     
            var infoWindow = new google.maps.InfoWindow({ maxWidth: 400 });        
            //create map object with default settings of location/zoom                
            var map = new google.maps.Map(document.getElementById('show_map'), {
              zoom: 10 ,
              center: {lat: show_map_nearbys[0]['latitude'], lng: show_map_nearbys[0]['longitude']}
            });
            //use the variable to create individual marker object using latlng.You may remove the icon 
            var markers = locations.map(function(location, i) {
                var marker = new google.maps.Marker({
                  position: location,
                  //copying as it from the documentation
                  icon: new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld="+first_letter+"|5c5cd6|ffffff&ext=.png", new google.maps.Size(24, 32));
                });
                //add onClick listener on each marker to show infowindow on click and bind the infowindow with the marker
                google.maps.event.addListener(marker, 'click', function(evt) {
                  infoWindow.setContent(location.info);
                  infoWindow.open(map, marker);
                })
                //get all markers binded with their infowindows respectively
                gmarkers.push(marker);
                return marker;
            });        
            
            // Add a marker clusterer to manage the markers.
            //markeraCluster is a global object available after including the markerClusterer.js script on your page
            var markerCluster = new MarkerClusterer(map, markers, {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});


Hope this helps someone.Happy coding.




         

Comments

Popular posts from this blog

Using Geocoder to get street name,state,city,country etc in Your Rails app

Recently i implemented Google map for one of the module where User will enter his location/address and other relevant geo-details should get autopopulated.Well,This was posible because of   Geocoder  and by using it you can get every small details such as postalcode,country code,zipcode,street name,state name,state code,latitude,longitude(and more) ready in your hands easily.Its really awesome. I have used to it get all relevant information about the location that the user enter using Geocomplete ,another awesome autocomplete library to get locations. Suppose you have a users table and also few columns such as longitude,latitude,state,postal code,street name,address,country  and want to autopopulate other fields just by using address entered by the user then you are reading the right blog,So its easy,let me show you how. User will enter address using Geocomplete and store that address in address column in users/locations table Then,use Geocoder to fetch other geo informations

Adding Emoji icons to Rails application

Its very easy to add emoji icons/images to your rails application. It seems complex but its all easy using javascript.The steps can be simplified as shown below:- add the javascript(download jemotion ) include it in your view file(where you want :) to be shown as this -----------------------> thats it....done first you need to add this wonderful js which works really great and nice from home page . We are interested in only the js and emotions folder(containing the smileys/GIF images). once you download just include it in your view file and copy/dump the emotions folder(containing images) to app/assets/images for RAILS 3+. as i did where i want the users to view their comments after they submit their comment. You also need to give a unique id to make  the js understand where it needs to convert your symbols to icons.see below code and hope you will understand how to use it. Keep in mind two thing:- dont forget to gi

Convert videos using ffmpeg by ffmpeg_paperclip-Rails

Recently i implemented video uploads at S3 using ffmpeg-paperclip and ffmpeg where my uploaded videos get converted into mp4,webm and ogg(most required formats) along with 2 screenshots(with diff sizes) and stored at S3. its so wonderful in many ways:- your uploaded videos get converted into required formats(compatible on all browsers) before saving using ffmpeg through ffmpeg-paperclip moreover,you will also get screenshot(2 different images)of every video uploaded + AMAZON S3( you can even store locally,refer Paperclip ) if you want the background job to process video conversion then,use delayed_paperclip  i will show you how you can convert your uploaded videos into required formats and why we need to convert ....well refer  this .Before starting make sure that you have ffpmeg installed with all libraries needed,so click the above link instead of searching for how to install ffmpeg and install it( will take around 30mins.but its worthy ). Here i am using mediaelement.