如何使用用户坐标更改起始坐标?

时间:2019-11-10 06:48:14

标签: html api google-maps latitude-longitude

我正在开发一个程序,该程序需要纬度和经度,并根据搜索查询生成附近的位置。一切都按预期工作,但是我对如何使用用户坐标更改起始坐标感到困惑。我最初的计划是让用户以以下形式输入经纬度:

<form id="mapCenterForm">
    <label for="latitude">lat</label>
    <input type="text" id="lat" name="latitude" placeholder="0.000000">

    <label for="longitude">lng</label>
    <input type="text" id="lng" name="longitude" placeholder="0.000000">
    <br>
    <input type="submit">
</form>

然后使用输入的信息来更改脚本中的变量lat和lng:

  var map;
  var lat= 34.16076;
  var lng= -70.20507;
  var map=new google.maps.LatLng(lat,lng);

      function initMap() {
        // Create the map.
        var SET = {lat, lng};
        map = new google.maps.Map(document.getElementById('map'), {
          center: SET,
          zoom: 10
        });

但是我不知道是否有替代地图加载的纬度和经度的方法。

很抱歉,如果这与发布的其他一些问题相似,我尝试使用Changing latitude and longitude google maps api作为指导,但对我没有太大帮助。


编辑:我收到答复并添加了完整的HTML代码,并进行了一些编辑,但实际上并没有更改SET变量。希望人们能够运行此程序,以便更好地了解问题。

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Base Mapper</title>
    <style>
      #map {
        height: 100%;
      }
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #right-panel {
        font-family: 'Roboto','sans-serif';
        line-height: 30px;
        padding-left: 10px;
      }

      #right-panel select, #right-panel input {
        font-size: 15px;
      }

      #right-panel select {
        width: 100%;
      }

      #right-panel i {
        font-size: 12px;
      }
      #right-panel {
        font-family: Arial, Helvetica, sans-serif;
        position: absolute;
        right: 5px;
        top: 60%;
        margin-top: -395px;
        height: 650px;
        width: 200px;
        padding: 5px;
        z-index: 5;
        border: 1px solid #999;
        background: #fff;
      }
      h2 {
        font-size: 22px;
        margin: 0 0 5px 0;
      }
      ul {
        list-style-type: none;
        padding: 0;
        margin: 0;
        height: 580px;
        width: 200px;
        overflow-y: scroll;
      }
      li {
        background-color: #f1f1f1;
        padding: 5px;
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
      }
      li:nth-child(odd) {
        background-color: #fcfcfc;
      }
      #more {
        width: 100%;
        margin: 5px 0 0 0;
      }
          input[type=text], select {
              width: 100%;
              padding: 12px 20px;
              margin: 8px 0;
              display: inline-block;
              border: 1px solid #ccc;
              border-radius: 4px;
              box-sizing: border-box;
          }

          .container {
              border-radius: 5px;
              background-color: #f2f2f2;
              padding: 20px;
              width:70%
          }

    </style>
    <div class="container">
      <form id="mapCenterForm">
        <label for="latitude">lat</label>
        <input type="text" id="lat" name="latitude" placeholder="0.000000">

        <label for="longitude">lng</label>
        <input type="text" id="lng" name="longitude" placeholder="0.000000">
        <br>
        <button onclick = "change_center()">
          Submit
        </button>
      </form>
        <div id="map" style="height: 500px"></div>

    </div>
    <script>
      var red_icon =  'http://maps.google.com/mapfiles/ms/icons/red-dot.png' ;
      var map;
      var lat= 41.18076;
      var lng= -73.20537;
      //THE FOLLOWING LINE WAS REMOVED
      //var map=new google.maps.LatLng(lat,lng);

      function initMap() {
        // Create the map.
        var SET = {lat, lng};
        map = new google.maps.Map(document.getElementById('map'), {
          center: SET,
          zoom: 13
        });

        function change_center() {
          var newLat = document.getElementById("lat").value;
          var newLng = document.getElementById("lng").value;
          var SET = {newLat, newLng};
          map.setCenter(new google.maps.LatLng(newLat, newLng));
        }

        // Create the places service.
        var service = new google.maps.places.PlacesService(map);
        var getNextPage = null;
        var moreButton = document.getElementById('more');
        moreButton.onclick = function() {
          moreButton.disabled = true;
          if (getNextPage) getNextPage();
        };

        // Perform a nearby search.
        service.nearbySearch(
            {location: SET, radius: 7500, keyword: "library"},

            function(results, status, pagination) {
              if (status !== 'OK') return;

              createMarkers(results);
              moreButton.disabled = !pagination.hasNextPage;
              getNextPage = pagination.hasNextPage && function() {
                pagination.nextPage();
              };
            });
      }

      function createMarkers(places) {
        var bounds = new google.maps.LatLngBounds();
        var placesList = document.getElementById('places');

        for (var i = 0, place; place = places[i]; i++) {
          var image = {
            url: place.icon,
            size: new google.maps.Size(71, 71),
            origin: new google.maps.Point(0, 0),
            anchor: new google.maps.Point(17, 34),
            scaledSize: new google.maps.Size(25, 25)
          };

          var marker = new google.maps.Marker({
            map: map,
            icon: red_icon,
            title: place.name,
            position: place.geometry.location
          });

          var li = document.createElement('li');
          li.textContent = place.name;
          placesList.appendChild(li);

          bounds.extend(place.geometry.location);
        }
        map.fitBounds(bounds);
      }
    </script>
  </head>
  <body>
    <div id="map"></div>
    <div id="right-panel">
      <h2>Locations</h2>
      <ul id="places"></ul>
      <button id="more">More Results</button>
    </div>
    <script src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY&libraries=places&callback=initMap" async defer></script>
  </body>
</html>

2 个答案:

答案 0 :(得分:0)

为了进行测试,您可以避免提交,而只需使用按钮而不是调用函数来更改中心

<form id="mapCenterForm">
    <label for="latitude">lat</label>
    <input type="text" id="lat" name="latitude" placeholder="0.000000">

    <label for="longitude">lng</label>
    <input type="text" id="lng" name="longitude" placeholder="0.000000">
    <br>
    <button onclick = "change_center()"> 
            Click Here for move map
    </button> 

</form>

var map;
var lat= 34.16076;
var lng= -70.20507;
var map=new google.maps.LatLng(lat,lng);

  function initMap() {
    // Create the map.
    var SET = {lat, lng};
    map = new google.maps.Map(document.getElementById('map'), {
      center: SET,
      zoom: 10
    });

  function change_center() {
    var newLat = document.getElementById("lat").value;
    var newLng = document.getElementById("lng").value;
    map.setCenter(new google.maps.LatLng(newLat, newLng));
}

答案 1 :(得分:0)

此问题的解决方案已添加到另一个有关更新标记的问题中:Google Maps: Resetting Markers with Coordinates