Giter Site home page Giter Site logo

cyphercodes / location-picker Goto Github PK

View Code? Open in Web Editor NEW
72.0 4.0 14.0 441 KB

An open source location picker plugin using Google Maps v3 that works with all JavaScript flavors!

Home Page: https://cyphercodes.github.io/location-picker/

License: GNU General Public License v3.0

HTML 21.26% TypeScript 78.74%
google-maps locationpicker javascript-plugin typescript javascript

location-picker's Introduction

πŸ—ΊπŸŽ― location-picker

Efficiently allow your users to pick a location!

Travis Open Source Love semantic-release npm latest version

location-picker allows you to quickly render Google Maps with an overlaying marker providing an easy and quick plug-and-play location picker. It uses Google Maps v3 and it works with all JavaScript flavors!

LIVE DEMO

DOCUMENTATION

Requirements

  • Google Maps v3

Installation

npm install location-picker --save

Import libraries using HTML:

From node_modules:

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key={ENTER YOUR KEY}"></script>
<script src="../node_modules/location-picker/dist/location-picker.min.js"></script>

From CDN:

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key={ENTER YOUR KEY}"></script>
<script src="https://unpkg.com/location-picker/dist/location-picker.min.js"></script>

Import using Typescript or Angular

import LocationPicker from "location-picker";

Import using CommonJS / Node:

var locationPicker = require("location-picker")

Usage

Add element in HTML with a unique id:

#map {
    width: 100%;
    height: 480px;
}
<div id="map"></div>

Initialize the locationPicker plugin:

Plain JavaScript:

var locationPicker = new locationPicker('map', {
    setCurrentPosition: true, // You can omit this, defaults to true
}, {
    zoom: 15 // You can set any google map options here, zoom defaults to 15
});

Angular:

let lp = new LocationPicker('map',{
    setCurrentPosition: true, // You can omit this, defaults to true
}, {
    zoom: 15 // You can set any google map options here, zoom defaults to 15
});

Methods

locationPicker(elementId, pluginOptions, mapOptions)

Returns a reference to the locationPicker object

element: string | HTMLElement

The ID of the HTML element you want to initialize the plugin on or a direct reference to the HTMLElement.

pluginOptions:

Options specific for this plugin

  • lat: latitude of initial needed position
  • lng: longitude of initial needed position
  • setCurrentPosition: specifies if you want the plugin to automatically try and detect and set the marker to the the current user's location. It has no effect if lat and lng are supplied. (defaults to true)

mapOptions:

You can set any specific google maps option here.

For a list of all the available options please visit:

https://developers.google.com/maps/documentation/javascript/reference#MapOptions

locationPicker.getMarkerPosition()

Returns an object that contains the lat and lng of the currently selected position.

Properties

locationPicker.element

A reference to the element the plugin was initialized on.

locationPicker.map

A reference to the Google Map object

Examples

HTML Full Example

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example</title>
  <script type="text/javascript"
          src="https://maps.googleapis.com/maps/api/js?key={{ENTER YOUR KEY}}"></script>
  <script src="https://unpkg.com/location-picker/dist/location-picker.min.js"></script>
  <style type="text/css">
    #map {
      width: 100%;
      height: 480px;
    }
  </style>
</head>

<body>
<div id="map"></div>
<br>
<button id="confirmPosition">Confirm Position</button>
<br>
<p>On idle position: <span id="onIdlePositionView"></span></p>
<p>On click position: <span id="onClickPositionView"></span></p>
<script>
  // Get element references
  var confirmBtn = document.getElementById('confirmPosition');
  var onClickPositionView = document.getElementById('onClickPositionView');
  var onIdlePositionView = document.getElementById('onIdlePositionView');

  // Initialize locationPicker plugin
  var lp = new locationPicker('map', {
    setCurrentPosition: true, // You can omit this, defaults to true
  }, {
    zoom: 15 // You can set any google map options here, zoom defaults to 15
  });

  // Listen to button onclick event
  confirmBtn.onclick = function () {
    // Get current location and show it in HTML
    var location = lp.getMarkerPosition();
    onClickPositionView.innerHTML = 'The chosen location is ' + location.lat + ',' + location.lng;
  };

  // Listen to map idle event, listening to idle event more accurate than listening to ondrag event
  google.maps.event.addListener(lp.map, 'idle', function (event) {
    // Get current location and show it in HTML
    var location = lp.getMarkerPosition();
    onIdlePositionView.innerHTML = 'The chosen location is ' + location.lat + ',' + location.lng;
  });
</script>

</body>
</html>

Angular Example

  • Import Google maps:

One example could be adding in index.html:

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key={{ENTER YOUR KEY}}"></script>
  • Add map element and button in HTML:
<div id="map"></div>
<button (click)="setLocation()">Submit Location</button>
  • Add this CSS:
#map {
    width: 100%;
    height: 480px;
}
  • Component:
import {Component} from '@angular/core';
import LocationPicker from "location-picker";

@Component({
  selector: 'page-location',
  templateUrl: 'location.html'
})
export class LocationPage implements OnInit {
   lp: LocationPicker;
   
   ngOnInit(){
     this.lp = new LocationPicker('map');
   }
   
   setLocation() {
      console.log(this.lp.getMarkerPosition());
   }
}

location-picker's People

Contributors

cyphercodes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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

location-picker's Issues

Cannot find namespace 'google' (Angular 7.2)

I've just followed instructions in readme.md "Angular Example" and this error occured:

ERROR in node_modules/location-picker/dist/types/location-picker.d.ts(1,23): error TS2688: Cannot find type definition file for 'googlemaps'.
node_modules/location-picker/dist/types/location-picker.d.ts(2,21): error TS2503: Cannot find namespace 'google'.
node_modules/location-picker/dist/types/location-picker.d.ts(3,14): error TS2503: Cannot find namespace 'google'.

Angular CLI: 7.2.3
Node: 8.15.0
OS: linux x64
Angular: 7.2.2

Passing long and lat to an HTML form and sending it to a different page

Thank you very much for your great contribution, everything works fine and I can display the result of the JavaScript inside this span in my html using the id value.

Now, what I want is to pass that value into an HTML form and send it to another page but i do not know how to do that

Add polygon

how can i add polygon with this lib .... can u provide some examples ..
image

Give the possibility to define our own default lat, lng instead of tripoli, liban

Being in France, having Tripoli, Liban as the default location seems weird.

I would like to be able to define my own.

I'm not an expert in JS but I guess that here : https://github.com/cyphercodes/location-picker/blob/master/src/location-picker.ts#L23

We could have something like :

center: new google.maps.LatLng(pO.lat ? pO.lat : pO.dfLat ? pO.dfLat : 34.4346, pO.lng ? pO.lng : pO.dfLng ? pO.dfLng : 35.8362),

What do you think ?

insert picker in google form with the option of uploading files

Hello I want to build a database for the end of studies project for my students
And I want to do that with google-form, but the option I want is to add a map with which the student can georeference his project on a map but easily so when he puts his project on a map the box georeferenced will fill in automatically, with the option to upload PDF, thank you very much for your help.
cheers

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.