0

我在 google maps api 的帮助下创建了 html 页面。但我不能为给定的地方放置标记。

我的要求是当我在自动完成框中输入位置时,单击搜索按钮后为给定位置放置标记。请任何人帮助我!

我的代码在这里:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Simple markers</title>
    <style>
      html, body, #map-canvas
      {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
      .controls
      {
        margin-top: 16px;
        border: 1px solid transparent;
        border-radius: 2px 0 0 2px;
        box-sizing: border-box;
        -moz-box-sizing: border-box;
        height: 32px;
        outline: none;
        box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
        background-color: #fff;
        padding: 0 11px 0 13px;
        width: 400px;
        font-family: Roboto;
        font-size: 15px;
        font-weight: 300;
        text-overflow: ellipsis;
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?YOUR_API_KEY&libraries=places"></script>
    <script>
      function initialize() {
        var myLatlng = new google.maps.LatLng(13.0839, 80.2700);
        var mapOptions = {
          zoom: 7,
          center: myLatlng
        }
        var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

        autocomplete();
      }

      function autocomplete()
      {
        var source = document.getElementById('start');

        var autocomplete = new google.maps.places.Autocomplete(source);
        autocomplete.bindTo('bounds', map);

      }

      function calcRoute() {
        var start = document.getElementById('start').value;

        var marker = new google.maps.Marker({
          position: start,
          map: map,
          title: 'Hello World!'
        });
      }
      google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
    <div id="panel">
      <b>Start: </b>
      <input id="start" class="controls" type="text"
             placeholder="Enter a search location">
      <input type="button" value="Route" onclick="calcRoute();">
    </div>
    <div id = "map-canvas"> </div>
  </body>
</html>

提前致谢

4

1 回答 1

0

你需要听place_changed。当用户从自动完成中选择一个位置时触发此事件,然后您可以将标记放置在该位置上。

此代码基于您可以在此处找到的 google 演示。

var map;

function initialize() {
  var myLatlng = new google.maps.LatLng(13.0839, 80.2700);
  var mapOptions = {
    zoom: 7,
    center: myLatlng
  }
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

  autocomplete();
}

var autocomplete;
var marker;
function autocomplete() {
  var source = document.getElementById('start');

  autocomplete = new google.maps.places.Autocomplete(source);
  autocomplete.bindTo('bounds', map);

  marker = new google.maps.Marker({
    map: map,
    anchorPoint: new google.maps.Point(0, -29)
  });

  autocomplete.addListener('place_changed', function() {
    marker.setVisible(false);
    var place = autocomplete.getPlace();
    if (!place.geometry) {
      window.alert("Autocomplete's returned place contains no geometry");
      return;
    }

    // If the place has a geometry, then present it on a map.
    if (place.geometry.viewport) {
      map.fitBounds(place.geometry.viewport);
    } else {
      map.setCenter(place.geometry.location);
      map.setZoom(17);  // Why 17? Because it looks good.
    }

    marker.setPosition(place.geometry.location);
    marker.setVisible(true);
  });
}

//function calcRoute() {
//  var start = document.getElementById('start').value;
//
//  var marker = new google.maps.Marker({
//    position: start,
//    map: map,
//    title: 'Hello World!'
//  });
//}


google.maps.event.addDomListener(window, 'load', initialize);
html, body, #map-canvas
{
  height: 100%;
  margin: 0px;
  padding: 0px
}
.controls
{
  margin-top: 16px;
  border: 1px solid transparent;
  border-radius: 2px 0 0 2px;
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  height: 32px;
  outline: none;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
  background-color: #fff;
  padding: 0 11px 0 13px;
  width: 400px;
  font-family: Roboto;
  font-size: 15px;
  font-weight: 300;
  text-overflow: ellipsis;
}
<script src="https://maps.googleapis.com/maps/api/js?YOUR_API_KEY&libraries=places"></script>
<div id="panel">
  <b>Start: </b>
  <input id="start" class="controls" type="text"
         placeholder="Enter a search location">
  <input type="button" value="Route" onclick="calcRoute();">
</div>
<div id = "map-canvas"> </div>

http://jsbin.com/jofovig/edit?html,css,js

于 2016-03-15T16:04:38.773 回答