首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >谷歌地图HTML5地理定位

谷歌地图HTML5地理定位
EN

Stack Overflow用户
提问于 2012-06-30 19:18:48
回答 1查看 1.4K关注 0票数 0

我一直在遵循一个HTML5地理位置教程,我想修改代码,以便有一个目的地地址的文本输入,而不是一个固定的目的地。在这段代码中实现它的任何方法都很容易实现。

//这里是java脚本代码

代码语言:javascript
运行
复制
(function(geolocation){

  if (geolocation) return;

  var cache;

  geolocation = window.navigator.geolocation = {};
  geolocation.watchPosition = function(callback){

    if (cache) callback(cache);

    $.getScript('//www.google.com/jsapi',function(){

      cache = {
        coords : {
          "latitude": google.loader.ClientLocation.latitude, 
          "longitude": google.loader.ClientLocation.longitude
        }
      };

      callback(cache);
    });

  };


})(navigator.geolocation);

// Proceed as usual.
(function() {
    if ( navigator.geolocation ) {
    var coords = navigator.geolocation.watchPosition( hereWeGooo,
        function() {
            // failure
                document.body.innerHTML = 'Sorry. We can\'t get your current location.';
            },
            { enableHighAccuracy: true }
        );

    }
function hereWeGooo(coords) {
    coords = coords.coords;
    var latlng = new google.maps.LatLng(coords.latitude, coords.longitude),
        myOptions = {
        //zoom: 15,
        //center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    },
    map = new google.maps.Map(document.querySelector( "#map"), myOptions),
      directionsDisplay = new google.maps.DirectionsRenderer(),
      directionsService = new google.maps.DirectionsService(),

        var dest = document.getElementById('d').value;

    request = {

        origin: latlng,
        destination: dest, 
        // replace with your own airport
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };

    directionsDisplay.setMap(map);
    directionsService.route(request, function(response, status) {
        if (status === google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
        }
        });
    }
})();

  <form>
    <p><input type="text" id="d" /></p>
    <input type="button" value="Go" onClick="hereWeGooo()">
    </form>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-30 22:56:09

你可以这样做:

代码语言:javascript
运行
复制
var dest = document.getElementById('myInput').value;

request = {
    origin: latlng,
    destination: dest, 
//...
}

...and然后在您的文档正文中:

代码语言:javascript
运行
复制
<input id='myInput'>

在看到链接后添加:

没有定义hereWeGooo(),因为您在应该有分号的地方使用了逗号- direction.js第57行崩溃。

第54到57行应该是:

代码语言:javascript
运行
复制
map = new google.maps.Map(document.querySelector( "#map"), myOptions); //<----- semi-colon, not comma
        directionsDisplay = new google.maps.DirectionsRenderer(); //<----- semi-colon, not comma
        directionsService = new google.maps.DirectionsService(); //<----- semi-colon, not comma
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11273655

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档