如何使用Google Maps API搜索框转到精确坐标?

时间:2018-01-08 00:09:27

标签: javascript api google-maps geocoding

我正在尝试使用Google Maps Javascript API地方搜索框找出如何移动到精确坐标。

使用文档here中的示例,我添加了一些警告:

// This example adds a search box to a map, using the Google Place Autocomplete
// feature. People can enter geographical searches. The search box will return a
// pick list containing a mix of places and predicted search terms.

// This example requires the Places library. Include the libraries=places
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">

function initAutocomplete() {
  var map = new google.maps.Map(document.getElementById('map'), {
    center: {
      lat: -33.8688,
      lng: 151.2195
    },
    zoom: 13,
    mapTypeId: 'hybrid'
  });

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

  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 = [];

    // For each place, get the icon, name and location.
    var bounds = new google.maps.LatLngBounds();
    places.forEach(function(place) {
      if (!place.geometry) {
        console.log("Returned place contains no geometry");
        return;
      }
      alert(place.geometry.location.lat());
      alert(place.geometry.location.lng());
      var icon = {
        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)
      };

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

如果我输入,例如, 47,-121 ,我会被带到 46.9905925,-120.9854728 - 距离我想去的地方有一段距离。 Google地图和Google地球都会将我带到正确的位置。

如何使地方搜索框正常工作?我尝试过使用DMS,DMM和DD格式,但它们无法可靠地工作。例如, 47°30'N,121°30'W 有效,但 47°0'N,121°0'W 不起作用(这不是去任何地方), 47.0000,-121.000

也不会

1 个答案:

答案 0 :(得分:0)

因此,无论出于何种原因,Google的默认搜索框实施仅支持坐标输入。正如评论中所建议的那样,有必要推出自己的解决方案。

使用this parsing code,您可以在找不到任何地点时作出回应:

var places = searchBox.getPlaces();
if (places.length == 0) {
  var input = document.getElementById('pac-input');
  var result = parseDMS(input.value);
  var bounds = new google.maps.LatLngBounds();
  var latlng = new google.maps.LatLng(result.lat, result.lon);
  bounds.extend(latlng);
  // Create a marker
  var marker = new google.maps.Marker({
    map: map,
    title: 'Coordinates',
    position: latlng
  });
  map.fitBounds(bounds);
  return;
}