Giter VIP home page Giter VIP logo

mapster's Introduction

Mapster

This is a Google Map Api utility tool which is basically an OO abstraction of Google Map API, Google Place Auto-Complete, Marker-Clusterer

Why should I care about this?

Think of this as a utility belt tool like Batman's but instead of catching the baddies, it's for handling Google Map Api, Google Place, and Marker Clusterer.

This is created as an OO high level absctraction tool that enables one to easily create a custom interactive map.

By separating the logic of handling the map object, and the list of items / states, it makes the code of creating a custom map to be more portable and easier to re-use on other projects.

Installation

ensure that the following script is included in your html page in the following order:

<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script> // Google map api with Google Place library enabled.
<script src='../src/markerClustererPlus.js'></script> // Marker clusterer plus library
<script src='../src/list.js'></script> // this script handles the list logic
<script src='../src/mapster.js'></script> // this script handles the map object
<script src='../src/mapOptions.js'></script> // this script contains the map configuration options

How do I use this?

To create map

create a MAP var that initiate the creation of the MAPSTER object. The map is created with the MAP_OPTIONS as specified in "mapOptions.js", and the map is hooked on the DOM element with ID 'map-canvas'

 var MAP = Mapster.create('map-canvas', Mapster.MAP_OPTIONS);

To hook Google Place AutoComplete

This will hook the Google Place Auto-Complete on the DOM input element with ID 'txtPlaces'

 MAP.setPlaces('txtPlaces', {
    events: [{
      name: 'place_changed',
      callback: function(e, places){
        var place = places.getPlace();
        console.log(place.geometry.location);
      }
    }]
  });

To use HTML5 and get user's geolocation

This will utilize HTML5 navigator function to get the user's geolocation if the feature is enabled, and then pan the map to the user's location (or to the default location set in mapOptions.js if the feature is disabled)

 MAP.getCurrentPosition( function(position) {
    var lat = position.coords.latitude,
        lng = position.coords.longitude;

    MAP.panTo({
      lat: lat,
      lng: lng
    });      

  });

Add marker on the map

This will add marker onto the map. We can pass in properties to the markers as object. The properties include event, custom styles, marker's properties, or additional properties we'd like to add onto the marker.

 MAP.addMarker({
      lat: -37.818667 + Math.random()/50, // Specifiy the lat prop of the marker
      lng: 144.971466 + Math.random()/50, // Specifiy the lng prop of the marker
      animation: google.maps.Animation.DROP, // add animation to the marker property
      content: 'I like my girl', // add content info to the marker property.
      venue_type: 'romantic', // add venue_type attribute on the marker
      venue_dog_friendly: 'yes' // add venue_dog_friendly attribute on the marker
});

Get a list of marker

The findBy function takes a callback that allows us to specify a condition. This returned callback will be passed to the List.js logic to filter the markers that satisfy the callback condition.

var myGirlMarkers = MAP.findBy(function(marker) { 
    return marker.content === 'I like my girl'
});

EXAMPLE

Refer to the example folder, or the live demo link

List of Mapster's functions

  • zoom() = get current map's zoom level.
  • zoom(num) = set current map's zoom level to specified num.
  • panTo(coord) = pan the map to the specified coord position {lat: xx, lng: xx}.
  • geocode(opts) = if geocoder = is enabled in mapOptions.js, we can pass in an address to the marker's address property to geocode the location through google map.
  • getCurrentPosition(callback) = Get user's current position. this function takes a callback that will be evaluated if the feature is enabled on the user's browser.
  • setPlaces(selector, opts) = Hook google place to the DOM element with id selector. by passing the callback we can specify the event we want to trigger, eg: on place_changed event.
  • addMarker(opts) = add maker onto the map, and passing in options.
  • findBy(callback) = find the markers that satisfy the condition in the evaluated callback function.
  • removeBy(callback) = remove the markers that satisfy the condition in the evaluated callback funciton.
  • removeAll() = remove all marker on the map.
  • visibleMarkersCount(selector) = count the visible markers on the map and render it on the DOM element with ID selector.

Credits:

mapster's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.