1

我只是试图替换 lat long 坐标以使用浏览器的地理位置,但我没有得到任何结果......

地理位置代码是;

    <script> var coords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    function initAutocomplete() {
    var map = new google.maps.Map(document.getElementById('map'), {
      center: coords,
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

其余代码指向 simple places api 服务;

    // Create the search box and link it to the UI element.
    var input = document.getElementById('pac-input');
    var searchBox = new google.maps.places.SearchBox(input);
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

    // Bias the SearchBox results towards current map's viewport.
    map.addListener('bounds_changed', function() {
      searchBox.setBounds(map.getBounds());
    });

    var markers = [];
    // Listen for the event fired when the user selects a prediction and retrieve
    // more details for that place.
    searchBox.addListener('places_changed', function() {
      var places = searchBox.getPlaces();

      if (places.length == 0) {
        return;
      }

      // Clear out the old markers.
      markers.forEach(function(marker) {
        marker.setMap(null);
      });
      markers = [];

        // Create a marker for each place.
        markers.push(new google.maps.Marker({
          map: map,
          icon: icon,
          title: place.name,
          position: place.geometry.location
        }));

        if (place.geometry.viewport) {
          // Only geocodes have viewport.
          bounds.union(place.geometry.viewport);
        } else {
          bounds.extend(place.geometry.location);
        }
      });
      map.fitBounds(bounds);
    });
  }

        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(success);
          } else {
            error('Geo Location is not supported');
          }

    </script>

我一生都找不到任何可靠的在线教程...

4

0 回答 0