在Google Places API中使用地理位置

时间:2016-05-24 00:14:24

标签: google-places-api google-geolocation

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

地理位置代码是;

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

其余代码指向简单的地方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>

我不能为我的生活找到任何可靠的在线教程...

0 个答案:

没有答案