Get Google Map Location (Lat/Lng): Simple JavaScript Location Picker
It renders a Google map and lets users pin a specific spot to retrieve the exact coordinates (Latitude and Longitude) via click or idle position.
Perfect for forms that require a location picker.
Include the Location Picker library and the Google Maps JavaScript API on the page.
<script src="https://maps.googleapis.com/maps/api/js?key=YouApiKeyHere"></script> <script src="/dist/location-picker.min.js"></script>
Create a placeholder element in which you want to render the Google Map.
<div id="map"></div>
Initialize the Location Picker.
var map = document.getElementById('map');
var instance = new locationPicker(map, {
// picker options
}, {
// Google Maps Options
}); Get the selected Location (Lat/Lng) on click: listen to the button event to retrieve the Google Map location coordinates:
<button id="confirmPosition">Confirm Position</button> <p>On click position: <span id="onClickPositionView"></span></p>
var confirmBtn = document.getElementById('confirmPosition');
var onClickPositionView = document.getElementById('onClickPositionView');
confirmBtn.onclick = function () {
var location = lp.getMarkerPosition();
onClickPositionView.innerHTML = 'The chosen location is ' + location.lat + ',' + location.lng;
}; Listen to the map idle event, listening to the idle event is more accurate than listening to the ondrag event. And then get the current location and show it in HTML.
<p>On idle position: <span id="onIdlePositionView"></span></p>
google.maps.event.addListener(lp.map, 'idle', function (event) {
var location = lp.getMarkerPosition();
onIdlePositionView.innerHTML = 'The chosen location is ' + location.lat + ',' + location.lng;
}); All default options for the location picker.
var instance = new locationPicker(map, {
// latitude
lat: 45.5017,
// longitude
lng: -73.5673,
// auto try and detect and set the marker to the the current user's location
setCurrentPosition: true
}, {
// Google Maps Options
}); 11/29/2025
The post Get Google Map Location (Lat/Lng): Simple JavaScript Location Picker appeared first on CSS Script.
In the ever-evolving world of malvertising, where bad actors continually refine their techniques, a new…
Retired Concord Circuit Court Judge Gerard Boyle has been nominated to be the next settlement…
Salisbury residents will be voting on a number of issues and candidates on March 10,…
Christopher Ellms Jr. received a 4-1 vote from the executive council on Wednesday to become…
Merrimack Valley voters will cast their ballots on March 5 in four School Board races,…
As libertarians affiliated with the Free State Project gather at an annual conference in Concord…
This website uses cookies.