【问题标题】:I want to get its current location first before a nearby places search and optionally after any search or before any search我想在附近的地方搜索之前首先获得它的当前位置,并且可以选择在任何搜索之后或任何搜索之前
【发布时间】:2019-08-27 08:54:28
【问题描述】:

我想在附近的地点搜索之前先获取它的当前位置,并且可以选择在任何搜索之后或任何搜索之前获取它的当前位置。我有一个关于如何获取其当前位置的代码,我不知道如何将其用作我刚才所说的问题。如何获取位置的代码也来自谷歌https://developers.google.com/maps/documentation/javascript/examples/map-geolocation,我可以将它与我的代码融合吗?在https://github.com/patrickesguerra/map

【问题讨论】:

    标签: javascript google-maps w3c-geolocation


    【解决方案1】:

    Google 的example 正在使用HTML5 Geolocation 获取用户的当前位置。

    要在您自己的代码实现中使用它,您只需修改您的 initialize 函数(您首先加载地图的位置)以将地图的 center 和附近搜索请求的 location 设置为用户的位置。

    见下面的代码:

    function initialize() {
      infowindow = new google.maps.InfoWindow();
      autocomplete = new google.maps.places.Autocomplete((document.getElementById('address')), {
        types: ['(regions)'],
        // componentRestrictions: countryRestrict
      });
    
      var pyrmont = new google.maps.LatLng(52.5666644, 4.7333304);
    
      map = new google.maps.Map(document.getElementById('map'), {
        zoom: 13
      });
    
      // HTML5 Geolocation
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
          var pos = {
            lat: position.coords.latitude,
            lng: position.coords.longitude
          };
          map.setCenter(pos);
    
          var service = new google.maps.places.PlacesService(map);
          service.nearbySearch({ location: pos, radius: 10000 }, callback);
    
        }, function() {
          // Map's center will default to pyrmont
          map.setCenter(pyrmont);
        });
      }
      else {
        // This browser doesn't support Geolocation
        // Map's center will default to pyrmont
        map.setCenter(pyrmont);
      }
    }
    

    希望这会有所帮助!

    【讨论】:

    • 我们如何在附近地点搜索之前或之后使用它?
    • 我想要的是在附近地点搜索之前或之后获取当前位置,我不知道如何使用您的代码将我的代码包含在附近地点搜索之前或之后。
    • 我可以用它来搜索附近的地方吗?
    • 哦,那么您真正想要做的是在实际附近搜索中使用用户的当前位置?是的,你绝对可以做到。
    • 在上面的答案中查看我的新编辑。复制并粘贴确切的功能。让我知道这是否是您真正的意思。
    猜你喜欢
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    相关资源
    最近更新 更多