Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
mappyland / vendor / assets / javascripts / mappyland.js
Size: Mime:
// Mappyland
// Mini-lib to generate a Google Map (API v3) with markers
// Author: Matthew Cowie for Neoteric Design
// www.neotericdesign.com

var Mappyland = function(container) {
  if(typeof(google) == "undefined"){
    console.log("Mappyland: This page doesn't seem to have the Google Maps API embedded :(");
    return false;
  }

  this.container = container;
  this.options = JSON.parse(this.container.dataset.options);

  if(typeof(this.options.autofitToMarkers) === "undefined")
    this.options.autofitToMarkers = true;

  this.gmap = new google.maps.Map(this.container, this.options.gmap_options);
  this.markers = {};
  this.currentInfowindow = new google.maps.InfoWindow();
  this.markerBounds = new google.maps.LatLngBounds();

  this.setupSpider();
  this.placeMarkers(this.loadMarkerData());
  this.setupClusters();

  if(this.options.autofitToMarkers)
    this.fitToMarkers();

  return;
};

Mappyland.prototype.findMarker = function(id) {
  return this.markers[id];
};

Mappyland.prototype.triggerMarkerClick = function(idOrMarker) {
  if(typeof(idOrMarker) === "object") {
    marker = idOrMarker;
  } else {
    marker = this.findMarker(idOrMarker);
  }

  google.maps.event.trigger(marker, 'click');
};

Mappyland.prototype.placeMarkers = function(markerDataSet){
  for(var idx in markerDataSet){
    this.createMarker(markerDataSet[idx]);
  }
};

Mappyland.prototype.fitToMarkers = function() {
  if(this.markerBounds.isEmpty())
    return false;

  this.gmap.fitBounds(this.markerBounds);
};

Mappyland.prototype.createMarker = function(markerData) {
  var coord = new google.maps.LatLng(markerData.latitude, markerData.longitude);

  this.markerBounds.extend(coord);

  iconPath = markerData.icon_path;
  if(!iconPath)
    iconPath = this.options.defaulIconPath;


  var marker = new google.maps.Marker({
    map: this.gmap,
    position: coord,
    title: markerData.title,
    icon: iconPath
  });

  if(typeof(this.spider) !== "undefined"){
    this.spider.addMarker(marker);
  }

  this.markers[markerData.id] = marker;

  var infowindow = new google.maps.InfoWindow({
    content: markerData.popup
  });

  google.maps.event.addListener(marker, 'click', function() {
    this.openInfoWindow(marker, infowindow);
  }.bind(this));


  return marker;
};

Mappyland.prototype.openInfoWindow = function(anchor, infowindow){

  this.closeInfoWindow();
  this.currentInfowindow = infowindow;
  infowindow.open(this.gmap, anchor);
};

Mappyland.prototype.closeInfoWindow = function() {
  this.currentInfowindow.close();
};

Mappyland.prototype.loadMarkerData = function() {
  data = JSON.parse(this.container.dataset.markers);
  // If marker data is not an array try reading it as a JSON object with root key
  if(!Array.isArray(data)){
    if(Object.keys(data).length == 1) {
      data = data[Object.keys(data)[0]];
    }
  }
  return data;
};

Mappyland.prototype.setupClusters = function() {
  if (typeof(MarkerClusterer) === "undefined")
    return false;

  this.clustered = new MarkerClusterer(this.gmap, this.markers);
  this.clustered.setMaxZoom(14);
};

Mappyland.prototype.setupSpider = function() {
  if (typeof(OverlappingMarkerSpiderfier) == "undefined")
    return false;

  this.spider = new OverlappingMarkerSpiderfier(this.gmap);
};

Mappyland.getDOMObject = function(objectOrId) {
  if(typeof objectOrId == 'string'){
    object = document.getElementById(objectOrId);
  } else if(typeof objectOrId == 'object'){
    object = objectOrId;
  } else {
    throw "Neither object nor id string provided";
  }

  return object;
};