Google maps Algolia和google根据用户位置过滤结果

Google maps Algolia和google根据用户位置过滤结果,google-maps,google-maps-api-3,jquery-animate,algolia,Google Maps,Google Maps Api 3,Jquery Animate,Algolia,您好,我正在使用谷歌地图和algolia,我有一个带有“lat”和“lng”的索引“locations” 我正在获取用户位置和观察位置,我还根据lng和lat显示数据库中的标记,但我想添加一点: 因此,我遵循了这一联系: 并提出: @extends('master') @section('title', 'Live Oldham') @section('extrafiles') <script type="text/javascript" src="https://maps.goog

您好,我正在使用谷歌地图和algolia,我有一个带有“lat”和“lng”的索引“locations”

我正在获取用户位置和观察位置,我还根据lng和lat显示数据库中的标记,但我想添加一点:

因此,我遵循了这一联系:

并提出:

 @extends('master') @section('title', 'Live Oldham')
@section('extrafiles')

<script type="text/javascript" src="https://maps.google.com/maps/api/js?v=3&key=AIzaSyAirYgs4Xnt9QabG9v56jsIcCNfNZazq50&language=en"></script>
<script type="text/javascript" src="{!! asset('js/homesearch.js') !!}"></script>
@endsection
@section('content')
        <div id="map_canvas" style="height:600px;"></div>
@endsection
@extends('master')@节('title','liveoldham'))
@节(“外部文件”)
@端部
@节(“内容”)
@端部
和js:

$(document).ready(function() {
var map;
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) {
    currentPositionMarker = new google.maps.Marker({
        map: map,
        position: new google.maps.LatLng(
            position.coords.latitude,
            position.coords.longitude
        ),
        title: "Current Position"
    });
    map.panTo(new google.maps.LatLng(
        position.coords.latitude,
        position.coords.longitude
    ));
}
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
console.log(latitude);
console.log(longitude);
function displayAndWatch(position) {
    // set current position
    setCurrentPosition(position);
    // watch position
    watchCurrentPosition(position);
    console.log(position);
}
function watchCurrentPosition(position) {
    var positionTimer = navigator.geolocation.watchPosition(
        function (position) {
            setMarkerPosition(
            currentPositionMarker,
            position,
        )
    });
}
function setMarkerPosition(marker, position) {
    marker.setPosition(
        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");
var fitMapToMarkersAutomatically = true;
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];
    console.log(hit)
    var marker = new google.maps.Marker({
      position: {lat: hit.longitude, lng: hit.latitude},
      map: map,
      title: hit.slug
    });


    markers.push(marker);
  }
  // Automatically fit the map zoom and position to see the markers
  if (fitMapToMarkersAutomatically) {
    var mapBounds = new google.maps.LatLngBounds();
    for (i = 0; i < markers.length; i++) {
      mapBounds.extend(markers[i].getPosition());
    }
    map.fitBounds(mapBounds);
  }

});
function renderHits(content) {
  $('#container').html(JSON.stringify(content, null, 2));
}
algoliaHelper.setQueryParameter('aroundRadius', 5000).search(); // 5km Radius

});
$(文档).ready(函数(){
var映射;
函数初始化映射(){
map=new google.maps.map(document.getElementById('map_canvas'){
缩放:19,
mapTypeId:google.maps.mapTypeId.ROADMAP
});
}
函数locError(错误){
//找不到当前位置
警报(“找不到当前位置!”);
}
功能设置当前位置(位置){
currentPositionMarker=新的google.maps.Marker({
地图:地图,
职位:新google.maps.LatLng(
位置坐标纬度,
位置坐标经度
),
标题:“当前职位”
});
map.panTo(新的google.maps.LatLng)(
位置坐标纬度,
位置坐标经度
));
}
变量纬度=位置坐标纬度;
var经度=position.coords.longitude;
控制台日志(纬度);
console.log(经度);
功能显示和观察(位置){
//设置当前位置
设置当前位置(位置);
//监视位置
手表当前位置(位置);
控制台日志(位置);
}
功能监视当前位置(位置){
var positionTimer=navigator.geolocation.watchPosition(
职能(职位){
设置标记位置(
当前位置标记,
立场,,
)
});
}
功能设置标记位置(标记,位置){
标记设置位置(
新google.maps.LatLng(
位置坐标纬度,
位置坐标经度)
);
}
函数initLocationProcedure(){
初始化映射();
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(displayAndWatch,locError);
}否则{
警报(“您的浏览器不支持地理位置API”);
}
}
$(文档).ready(函数(){
initLocationProcedure();
});
var应用程序_ID='75RQSC1OHE';
var搜索仅适用于API键='f2f1e9bba4d7390fc61523a04685cf12';
var索引_名称='位置';
var PARAMS={hitsPerPage:100};
//客户端+助手初始化
var algolia=algoliasearch(应用程序ID、仅搜索API密钥);
var algoliaHelper=algoliasearchHelper(algolia,索引名称,参数);
//地图初始化
var标记=[];
//警惕(“heelo”);
var fitmaptomarkerautomatically=true;
algoliaHelper.on('result',函数(content){
报告(内容);
var i;
//将标记添加到地图中
对于(i=0;i
但是,我不知道如何解决这方面的一些问题:

  • 当用户移动时,它不会将地图居中放置在标记上
  • 此时,当用户移动时,标记在位置之间跳跃,我希望标记在用户移动时在地图上动态移动
  • 我想使用algolia动态设置标记,所以我想显示距离用户位置5公里半径的标记,并动态添加或删除位于用户位置之外的标记

  • 在这些问题上我帮不了你多少忙,因为主要是关于如何使用GMAPJSlib的,我对它没有经验。然而,另一件事吸引了我的目光:

    var marker = new google.maps.Marker({
          position: {lat: hit.longitude, lng: hit.latitude},
          map: map,
          title: hit.slug
        });
    
    为了能够使用地理搜索功能,您应该在
    \u geoloc
    字段中输入坐标。看起来是这样的:

    _geoloc: {
          lat: 40.639751,
          lng: -73.778925
        }