0

我正在尝试学习如何使用 google api 代码来使用我正在创建的测试应用程序。最终结果是我想要一个按钮,它将使用用户当前位置加载方向到 JSON 文件中保存的坐标

我正在使用下面的谷歌示例,但我看不到让我当前的位置在其中工作或获取运输说明?

干杯

谷歌地图:

 <!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Travel modes in directions</title>
    <link href="https://developers.google.com/maps/documentation/javascript/examples/default.css" rel="stylesheet">
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
     <style>
      #directions-panel {
        height: 100%;
        float: right;
        width: 390px;
        overflow: auto;
      }

      #map-canvas {
        margin-right: 400px;
      }

      #control {
        background: #fff;
        padding: 5px;
        font-size: 14px;
        font-family: Arial;
        border: 1px solid #ccc;
        box-shadow: 0 2px 2px rgba(33, 33, 33, 0.4);
        display: none;
      }

      @media print {
        #map-canvas {
          height: 500px;
          margin: 0;
        }

        #directions-panel {
          float: none;
          width: auto;
        }
      }
    </style>
<script>
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var haight = new google.maps.LatLng(37.7699298, -122.4469157);
var oceanBeach = new google.maps.LatLng(37.7683909618184, -122.51089453697205);

function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer();
  var mapOptions = {
    zoom: 14,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: haight
  }
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  directionsDisplay.setMap(map);
  directionsDisplay.setPanel(document.getElementById('directions-panel'));
}

function calcRoute() {
  var selectedMode = document.getElementById('mode').value;
  var request = {
      origin: haight,
      destination: oceanBeach,
      // Note that Javascript allows us to access the constant
      // using square brackets and a string value as its
      // "property."
      travelMode: google.maps.TravelMode[selectedMode]
  };
  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>
    <div id="panel">
    <b>Mode of Travel: </b>
    <select id="mode" onchange="calcRoute();">
      <option value="DRIVING">Driving</option>
      <option value="WALKING">Walking</option>
      <option value="BICYCLING">Bicycling</option>
      <option value="TRANSIT">Transit</option>
    </select>
    </div>
    <div id="map-canvas"></div>
  </body>
</html>

JSON文件:

 {
    "Town":"Livingston",
    "Long":-3.52207,
    "Lat":55.8864,
  },
  {
    "Town":"Brighton and Hove Albion",
    "Long":-0.08014,
    "Lat":50.8609,
  },
  {
    "Town":"Liverpool",
    "Long":-2.96096,
    "Lat":53.4308,
4

1 回答 1

0

对于路线说明,您需要遍历directionsService. 就像是:

var steps = response.routes[0].legs[0].steps;
var html = '';
for (var i in steps) {
  html += steps[i].instructions + '<br/>';
}
document.getElementById('directions').innerHTML = html;

要获得您当前的位置,您的浏览器需要支持geolocation,无论是本机还是通过使用polyfill。你会像这样使用它:

var geo = window.navigator.geolocation;
if (geo) {
  geo.getCurrentPosition(showPos, showErr);
}

function showPos(pos) {
  var lat = pos.coords.latitude;
  var lng = pos.coords.longitude;
  console.log(lat, lng);
}

function showErr(err) {
  console.log(err);
}
于 2013-09-30T15:59:49.787 回答