Git Product home page Git Product logo

google-map-geofence's Introduction

Google Maps Geofence

geofence.js - Google maps geofence api in a more sophisticated way.

Click for demo https://sujancse.github.io/geofence/

How to Use

  1. Add a reference to Google Maps API
  2. Add gmap-v3-geofence.min.js in your HTML
  3. And don't forget to add the Google Maps API key
<!DOCTYPE html>
<html>
<head>
  <title>Geofence Demo</title>
  <script src="http://maps.google.com/maps/api/js?key=YOUR_API_KEY"></script>
  <script src="gmap-v3-geofence.js"></script>
  <style type="text/css">
    #mymap {
      width: auto;
      height: 450px;
    }
  </style>
</head>
<body>
  <div id="mymap"></div>
  <script>
  
    var coords = [
        [23.832570352277692,90.41199964550788],
        [23.798165936805923,90.38352334497074],
        [23.79768310017011,90.43916111914064]
    ];
    
    var geofence = new Geofence({mapId: 'mymap'});
    
    geofence.draw(coords);
    
  </script>
</body>
</html>

Geofence Events

Events list

  • nodeClick
  • polygonClick
  • dragStart
  • dragEnd
  • setAt
  • insertAt

Example Usage

Click on the node and delete

geofence.on('nodeClick', [polygon], function (event, polygon) {
    // Do something with the event and clicked polygon
    
    // Your can show infoWindow or marker
    var contentString = '<button id="delete">Delete</button>';
    infoWindow = geofence.createInfowindow(contentString, event);
    
    // Then on infowindow ready click delete to delete the node
    geofence.onDomReady(infoWindow, function() {
        selectId('delete').addEventListener('click', function() {
            geofence.close(infoWindow);
            geofence.deleteNode(event, polygon);
        });
    });
});

Drag end revert the polygon

geofence.on('dragEnd', [polygon], function (event, polygon, previousState) {
    
    // Show the infowindow
    var contentString = '<button id="revert">Revert</button>';
    infoWindow = geofence.createInfowindow(contentString, event);
    
    // On infowindow ready revert the polygon to previous state
    geofence.onDomReady(infoWindow, function() {
        selectId('revert').addEventListener('click', function() {
            geofence.close(infoWindow);
            geofence.revert(polygon, previousState);
        });
    });
});

API functions

Draw the polygon

/**
 * Draw polygon using coordinates
 *
 * @param coordinates
 * @param options optional
 * @returns {*}
 */
 
geofence.draw(coords, options);

Polygon events

On polygon node click

/**
* On node click will give you the event
* which includes the node coordinates
* See demo example how to use that coordinates
*/

geofence.on('nodeClick', [polygon], function (event, polygon) {
    // Do something with the node and polygon
});

On polygon click

/**
* On click will give you the event
* which includes the clicked coordinates
* See demo example how to use that coordinates
*/

geofence.on('polygonClick', [polygon], function (event, polygon) {
    // Do something with the event and polygon
});

Delete the clicked node

/**
* Delete the node by passing the node clicked event 
* and the polygon object
*
* Use this function inside on node click event
*/

geofence.deleteNode(event, polygon);

Drag the polygon and catch the event

/**
* Polygon dragStart will give you the event
* which includes the current coordinates
*/

geofence.on('dragStart', function(event, polygon) {
    // Do somethong with the event and polygon
});

Polygon dragEnd event

/**
* Polygon dragEnd will give you the event, current polygon
* and the previous polygon state
*
* See the advanced demo to know
* How to get back to the previous demo
*/

geofence.on('dragEnd', function(event, polygon, previousState) {
    // Do somethong with the event, polygon and previousState
});

Polygon insertAt and setAt event

/**
* Polygon insertAt and setAt will give you the event and polygon
*
* You get the polygon coordinates and events
*/

geofence.on('insertAt', function(event, polygon) {
    // Do somethong with the event and polygon
});

geofence.on('setAt', function(event, polygon) {
    // Do somethong with the event and polygon
});

Create InfoWindow and Marker

Create an infoWindow

/**
* Provide the content string
* Make sure you use this inside some events
*/

geofence.createInfowindow(contentString, event);

geofence.close(infoWindow);

Create a marker

/**
* Provide the title
* Make sure you use this inside some events
*/

geofence.createMarker(title, event);

geofence.closeMarker(marker);

General helper functions

Revert the polygon to previous state

/**
* Revert the polygon to previous state
* Make sure to use inside dragEnd event
*/

geofence.revert(polygon, previousState);

Get the polygon coordinates

/**
 * Get the polygon coordinates
 *
 * @param polygon object
 * @returns {*|Array}
 */
 
geofence.getCoordinates(polygon);

Get the polygon coordinates string

/**
 * Get the polygon coordinates string
 *
 * @param polygon object
 * @returns string
 */
 
geofence.getCoordinatesString(polygon);

Coordinates string to array

/**
 * Get the polygon coordinates string to array
 *
 * @param polygon object
 * @returns array
 */
 
geofence.stringToArray(coordinatesString);

Coordinates to LatLng object

/**
 * Coordinates to LatLng object
 *
 * @param polygon object
 * @returns LatLng object
 */
 
geofence.coordinatesToLatLng(coordinates);

google-map-geofence's People

Contributors

sujancse avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

google-map-geofence's Issues

getting null in "selectId(infoId).value"

I am getting null in the following function:

Geofence.prototype.setInfo = function(polygon, infoId) { selectId(infoId).value = getCoordinatesString(polygon); }

selectId(infoId).value

gmap-v3-geofence.js:208 Uncaught TypeError: Cannot set property 'value' of null
at Geofence.setInfo (gmap-v3-geofence.js:208)
at drawPolygon (geofencing.js:27)
at geofencing.js:17

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.