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

How I can change the starting coordinates with user coordinates?

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

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

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

<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));
}

在另一个关于更新标记的问题中添加了针对此问题的解决方案: