简体   繁体   中英

How to get the latitude and longitude from a Google Map marker in order to update in a form?

I want to make a form that auto-updates the coordinates of a marker. Here's my part of the script:

google.maps.event.addListener(map, "click", function(event) {
  marker = new google.maps.Marker({
    position: event.latLng,
    map: map,
  });
  var x = document.getElementById("coordinates");
  if (x.style.display === "none") {
    x.style.display = "block";
  };
  var lati, longi;
  lati = event.latLng;
  longi = event.latLng;
  $("#lat").val(lati);
  $("#lng").val(longi);
});

And here's the form:

<div id="coordinates" class="container" style="margin-top: 10px; display: none;">
  <div class="col-sm-5 col-md-5 col-lg-5">
    <input class="form-control" id="lat" name="lat" placeholder="Latitude" type="text" required>
  </div>
  <div class="col-sm-5 col-md-5 col-lg-5">
    <input class="form-control" id="lng" name="lng" placeholder="Longitude" type="text" required>
  </div>

You need to separate them using .lat()/.lng() :

var myLatLng = event.latLng;

var lati = myLatLng.lat();
var lngi = myLatLng.lng();

$("#lat").val(lati);
$("#lng").val(longi);
_^______________________ //Make sure you correct the typo here

You need to use the .lat() and .lng() methods of the google.maps.LatLng object to get the individual fields:

var lati, longi;
lati = event.latLng.lat();
longi = event.latLng.lng();
$("#lat").val(lati);
$("#lng").val(longi);

proof of concept fiddle

生成的地图的屏幕截图

code snippet:

 function initialize() { var map = new google.maps.Map( document.getElementById("map_canvas"), { center: new google.maps.LatLng(37.4419, -122.1419), zoom: 13, mapTypeId: google.maps.MapTypeId.ROADMAP }); google.maps.event.addListener(map, "click", function(event) { marker = new google.maps.Marker({ position: event.latLng, map: map, }); var x = document.getElementById("coordinates"); if (x.style.display === "none") { x.style.display = "block"; }; var lati, longi; lati = event.latLng.lat(); longi = event.latLng.lng(); $("#lat").val(lati); $("#lng").val(longi); }); } google.maps.event.addDomListener(window, "load", initialize); 
 html, body, #map_canvas { height: 95%; width: 100%; margin: 0px; padding: 0px } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maps.googleapis.com/maps/api/js"></script> <div id="coordinates" class="container" style="margin-top: 10px; display: none;"> <div class="col-sm-5 col-md-5 col-lg-5"> <input class="form-control" id="lat" name="lat" placeholder="Latitude" type="text" required> </div> <div class="col-sm-5 col-md-5 col-lg-5"> <input class="form-control" id="lng" name="lng" placeholder="Longitude" type="text" required> </div> </div> <div id="map_canvas"></div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM