如何使用setPanel()在javascript,google地图中添加地方搜索框和文字路线

时间:2015-07-05 11:50:12

标签: javascript google-maps google-maps-api-3

我尝试将setPanel()添加到地点搜索和文字方向。但我无法做到。

我想一起添加这些选项。

setPanel()placeSearch在一起。

我是google map api的新手。有谁可以帮助我?

这是我的代码

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Directions service</title>
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
      #panel {
        position: absolute;
        top: 5px;
        left: 50%;
        margin-left: -180px;
        z-index: 5;
        background-color: #fff;
        padding: 5px;
        border: 1px solid #999;
      }

      #pac-input {
        background-color: #fff;
        font-family: Roboto;
        font-size: 15px;
        font-weight: 300;
        margin-left: 12px;
        padding: 0 11px 0 13px;
        text-overflow: ellipsis;
        width: 400px;
      }

      #pac-input:focus {
        border-color: #4d90fe;
      }

      .pac-container {
        font-family: Roboto;
      }
#type-selector {
        color: #fff;
        background-color: #4d90fe;
        padding: 5px 11px 0px 11px;
      }

      #type-selector label {
        font-family: Roboto;
        font-size: 13px;
        font-weight: 300;
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places"></script>
    <script>
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();

//var places;

function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer();
  var chennai = new google.maps.LatLng(13.0475604,80.2089535);
  var mapOptions = {
    zoom:7,
    center: chennai
  };
  var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  directionsDisplay.setMap(map);

  // Create the search box and link it to the UI element.
 var input = /** @type {HTMLInputElement} */(
      document.getElementById('pac-input'));
      map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

  var searchBox = new google.maps.places.SearchBox(
    /** @type {HTMLInputElement} */(input));

  // [START region_getplaces]
  // Listen for the event fired when the user selects an item from the
  // pick list. Retrieve the matching places for that item.
  google.maps.event.addListener(searchBox, 'places_changed', function() {
   var places = searchBox.getPlaces();
    alert(places);

});
}

function calcRoute() {
  var start = places;
  var end = new google.maps.LatLng(41.850033, -87.6500523);
  var request = {
      origin:start,
      destination:end,
      travelMode: google.maps.TravelMode.DRIVING
  };
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    }
  });
}

google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>
  <body>

  <input id="pac-input" onchange="calcRoute();" class="controls" type="text" placeholder="Search Box">
    <div id="panel"></div>
    <div id="map-canvas"></div>
  </body>
</html>

1 个答案:

答案 0 :(得分:1)

好的,当你使用谷歌FormElement时,你不必在输入元素中调用calcRoute()。

只需使用以下代码即可。

1)Google表单提交:

    var places = searchBox.getPlaces();
    calcRoute(places[0].geometry.location);

2)calcRoute功能的第一个参数:

    var start = new google.maps.LatLng(places.A,places.F);

3)如果你想使用setpanel()方法,只需将此代码放在directionsDisplay.setMap(map);

下面

这将有效。

    directionsDisplay.setPanel(document.getElementById('panel'));