【问题标题】:Google maps display marker title when it appears on map谷歌地图出现在地图上时会显示标记标题
【发布时间】:2017-06-28 20:27:54
【问题描述】:

问题1

所以我有一个带有标记和用户位置的地图,标记在地图上,但我想要做的是当用户移动时,一个标记出现在他的路上,弹出它的标题,这样它就可以在不点击的情况下看到,并在标记不再可见时使其消失。

如何做到这一点?

问题2:

如何显示标题、从用户位置到该标记的距离?

这是我的代码:

$(document).ready(function() {
var map;
var circle;
      var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
        var icons = {
          parking: {
            icon: iconBase + 'parking_lot_maps.png'
          },
          library: {
            icon: iconBase + 'library_maps.png'
          },
          info: {
            icon: iconBase + 'info-i_maps.png'
          }
        };
function initializeMap(){
    map = new google.maps.Map(document.getElementById('map_canvas'), {
        zoom: 19,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });
}
function locError(error) {
// the current position could not be located
    alert("The current position could not be found!");
}
function setCurrentPosition(position) {
    var accuracy = position.coords.accuracy;
    currentPositionMarker = new google.maps.Marker({
        map: map,
        position: new google.maps.LatLng(
            position.coords.latitude,
            position.coords.longitude
        ),
        title: "Current Position",
        center: position,
        icon: iconBase + 'parking_lot_maps.png',
        animation: google.maps.Animation.DROP
    });
    map.panTo(new google.maps.LatLng(
        position.coords.latitude,
        position.coords.longitude
    ));
        circle = new google.maps.Circle({
        map: map,
  radius: accuracy,    // 10 miles in metres
  fillColor: '#255ebas'
});

circle.bindTo('center', currentPositionMarker, 'position')
}

function displayAndWatch(position) {
    // set current position
    setCurrentPosition(position);
    // watch position
    watchCurrentPosition(position);
}
function watchCurrentPosition(position) {
    var positionTimer = navigator.geolocation.watchPosition(
        function (position) {
            setMarkerPosition(
            currentPositionMarker,
            position,
        )
    });
}
function setMarkerPosition(marker, position) {
    circle.setRadius(position.coords.accuracy);
    marker.setPosition(
        new google.maps.LatLng(
            position.coords.latitude,
            position.coords.longitude)
    );
        map.panTo(new google.maps.LatLng(
        position.coords.latitude,
        position.coords.longitude
    ));

}
function initLocationProcedure() {
    initializeMap();
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(displayAndWatch, locError);
        }else{
            alert("Your browser does not support the Geolocation API");
    }
}

$(document).ready(function() {
    initLocationProcedure();
});
var APPLICATION_ID = '75RQSC1OHE';
var SEARCH_ONLY_API_KEY = 'f2f1e9bba4d7390fc61523a04685cf12';
var INDEX_NAME = 'locations';
var PARAMS = { hitsPerPage: 100 };
// Client + Helper initialization
var algolia = algoliasearch(APPLICATION_ID, SEARCH_ONLY_API_KEY);
var algoliaHelper = algoliasearchHelper(algolia, INDEX_NAME, PARAMS);

// Map initialization
var markers = [];
//alert("heelo");
algoliaHelper.on('result', function(content) {
    renderHits(content);
  var i;
  // Add the markers to the map
  for (i = 0; i < content.hits.length; ++i) {
    var hit = content.hits[i];
    var marker = new google.maps.Marker({
      position: {lat: hit.longitude, lng: hit.latitude},
      map: map,
      title: hit.slug,
      animation: google.maps.Animation.DROP
    });

    markers.push(marker);
  }

});
function renderHits(content) {
  $('#container').html(JSON.stringify(content, null, 2));
}
algoliaHelper.search();

});

【问题讨论】:

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


    【解决方案1】:
    1. 这里你需要听地图上的移动,然后更新边界框:

      map.addListener('bounds_changed', function() {
        var bounds = map.getBounds();
        algoliaHelper.setQueryParameter("insideBoundingBox", bounds.toUrlValue()).search();
      });
      

    关于 algolia 文档的更多信息:https://www.algolia.com/doc/guides/geo-search/geo-search-overview/#filter-inside-an-area

    1. Algolia 不提供到中心的距离,因此需要按照此堆栈溢出答案中的说明进行计算:https://stackoverflow.com/a/27943/654253

    【讨论】:

    • 嗨,我如何在移动中收听并运行搜索假设我想查看 200 米半径内的所有内容。我的 algolia 地理搜索也不起作用,因为它不在 _geo 变量中,它必须在其中吗?
    • 包含记录位置的数据必须在_geoloc属性中。我建议您阅读本指南,该指南非常详尽:algolia.com/doc/guides/geo-search/geo-search-overview
    猜你喜欢
    • 2016-02-08
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 2013-05-13
    相关资源
    最近更新 更多