Google Map get current location that can be changed by dragging the pin

ray

This is my first post so i'm sorry if it has been asked before.

I have a form users fill out on mobile devices which captures there longitude and latitude using JavaScript geolocation. this isn't always the most accurate so I am trying to combine geolocation and google map draggable pin.

So the user opens the page, geolocation enters the current lon and lat in the script below, if its wrong they can then drag the pin to the right location which updates the text fields.

I have tried every thing and have had mixed results...if someone could help me out that would be great.

I hope this makes sense...if not im happy to explain more

The script below enables you to drag the pin and updates the two text field with the lon and lat.

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  
  <input type="text" id="latitude" placeholder="latitude">
  <input type="text" id="longitude" placeholder="longitude">
  <div id="map" style="width:500px; height:500px"></div>
  
  <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
  <script>
	function initialize() {
		var $latitude = document.getElementById('latitude');
		var $longitude = document.getElementById('longitude');
		var latitude = 50.715591133433854
		var longitude = -3.53485107421875;
		var zoom = 7;
		
		var LatLng = new google.maps.LatLng(latitude, longitude);
		
		var mapOptions = {
			zoom: zoom,
			center: LatLng,
			panControl: false,
			zoomControl: false,
			scaleControl: true,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		}	
		
		var map = new google.maps.Map(document.getElementById('map'),mapOptions);
      
		
		var marker = new google.maps.Marker({
			position: LatLng,
			map: map,
			title: 'Drag Me!',
			draggable: true
		});
		
		google.maps.event.addListener(marker, 'dragend', function(marker){
			var latLng = marker.latLng;
			$latitude.value = latLng.lat();
			$longitude.value = latLng.lng();
		});
		
		
	}
	initialize();
	</script>
  
</body>
</html>

Emmanuel Delay
<script src="http://maps.googleapis.com/maps/api/js"></script>
<script type="text/javascript">
    function initialize() {
      var map;
      var position = new google.maps.LatLng(50.45, 4.45);    // set your own default location.
      var myOptions = {
        zoom: 15,
        center: position
      };
      var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);

      // We send a request to search for the location of the user.  
      // If that location is found, we will zoom/pan to this place, and set a marker
      navigator.geolocation.getCurrentPosition(locationFound, locationNotFound);

      function locationFound(position) {
        // we will zoom/pan to this place, and set a marker
        var location = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
        // var accuracy = position.coords.accuracy;

        map.setCenter(location);
        var marker = new google.maps.Marker({
            position: location, 
            map: map, 
            draggable: true,
            title: "You are here! Drag the marker to the exact location."
        });
        // set the value an value of the <input>
        updateInput(location.lat(), location.lng());

        // Add a "drag end" event handler
        google.maps.event.addListener(marker, 'dragend', function() {
          updateInput(this.position.lat(), this.position.lng());
        });


      }

      function locationNotFound() {
        // location not found, you might want to do something here
      }

    }
    function updateInput(lat, lng) {
      document.getElementById("my_location").value = lat + ',' + lng;
    }
    google.maps.event.addDomListener(window, 'load', initialize);
</script>
<style>
#map-canvas {
  width: 500px;
  height: 400px;
}
</style>
<div id="map-canvas"></div>
Location:<br>
<input id="my_location" readonly="readonly">

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to get current location in google map android

From Dev

Point current location on google map api and fetch details about it

From Dev

google map ios sdk: could not get current user location?

From Dev

Default current location pin in Skobbler map

From Dev

How to get location from Google Map after dropping Pin over it

From Dev

Not able to get current location in Google Map fragment

From Dev

How to get screen center location of google map?

From Dev

Google Map is not animating to current location on resuming

From Dev

How can I show current location on a Google Map on Android Marshmallow?

From Dev

Annotation Pin for current location not appearing

From Dev

Keep marker/pin in the middle of the map while dragging

From Dev

How can I get the current GPS location?

From Dev

Get Marker Location From Current Location Android Google Map

From Dev

Google Map get current location that can be changed by dragging the pin

From Dev

How to get screen center location of google map?

From Dev

Android: Not able to get current location in Google Map fragment

From Dev

How to set listener for current location pin on mapview?

From Dev

Android Google Map get correct location not work

From Dev

Show my current location in the Google Map

From Dev

Can't get current location android on location changed

From Dev

Get map location of viewport with angular google maps

From Dev

How to get Current location through google API?

From Dev

How to auto update current location on google map in ionic 2 application

From Dev

How remove default google map current location icon?

From Dev

How can I click on a pin in a Google map embedded from JavaScript?

From Dev

Set google map to current location

From Dev

button click show current location in google map

From Dev

Can not get my current location

From Dev

Unable to get current location with google map

Related Related

  1. 1

    how to get current location in google map android

  2. 2

    Point current location on google map api and fetch details about it

  3. 3

    google map ios sdk: could not get current user location?

  4. 4

    Default current location pin in Skobbler map

  5. 5

    How to get location from Google Map after dropping Pin over it

  6. 6

    Not able to get current location in Google Map fragment

  7. 7

    How to get screen center location of google map?

  8. 8

    Google Map is not animating to current location on resuming

  9. 9

    How can I show current location on a Google Map on Android Marshmallow?

  10. 10

    Annotation Pin for current location not appearing

  11. 11

    Keep marker/pin in the middle of the map while dragging

  12. 12

    How can I get the current GPS location?

  13. 13

    Get Marker Location From Current Location Android Google Map

  14. 14

    Google Map get current location that can be changed by dragging the pin

  15. 15

    How to get screen center location of google map?

  16. 16

    Android: Not able to get current location in Google Map fragment

  17. 17

    How to set listener for current location pin on mapview?

  18. 18

    Android Google Map get correct location not work

  19. 19

    Show my current location in the Google Map

  20. 20

    Can't get current location android on location changed

  21. 21

    Get map location of viewport with angular google maps

  22. 22

    How to get Current location through google API?

  23. 23

    How to auto update current location on google map in ionic 2 application

  24. 24

    How remove default google map current location icon?

  25. 25

    How can I click on a pin in a Google map embedded from JavaScript?

  26. 26

    Set google map to current location

  27. 27

    button click show current location in google map

  28. 28

    Can not get my current location

  29. 29

    Unable to get current location with google map

HotTag

Archive