简体   繁体   English

Google地方搜索标记未显示在地图上

[英]Google place search markers not showing on map

I'm coding a demo of how I want a future search function to work. 我正在编写一个演示程序,说明我希望将来的搜索功能如何工作。 I want to query google on an address or area and return the area bounds as well as nearby places. 我想查询一个地址或区域上的google,并返回区域范围以及附近的地方。 To do this, I am using places autocomplete, geocode, and places search. 为此,我正在使用地点自动填充,地理编码和地点搜索。

So far I am successfully getting predicted search queries resulting in the bounds being drawn as a rectangle on my map. 到目前为止,我已经成功地获得了预测的搜索查询,从而将边界绘制为地图上的矩形。 However, when I try to implement markers for the place search result no markers are appearing on my map. 但是,当我尝试为地点搜索结果实现标记时,我的地图上没有标记。 I know the search is working because putting an alert in the createMarker function on each location returns several lat/lng's that coincide with my area. 我知道搜索有效,因为在每个位置的createMarker函数中放置一个警报会返回与我所在地区相符的几个经度/纬度。

I suspect that maybe the map object is not being passed to my createMarker function, but I am kind of a noob when it comes to Javascript. 我怀疑也许地图对象没有传递给我的createMarker函数,但是当涉及到Javascript时我有点菜鸟。 I tried passing an additional parameter but it didn't do much. 我尝试传递一个附加参数,但没有做太多事情。

It should be noted that I am able to create a marker within the initialize function, when I attempt to just create one static marker. 应该注意的是,当我尝试仅创建一个静态标记时,便能够在初始化函数中创建标记。

EDIT: I have removed the type parameter for the place search request, but the code doesn't work even with the parameter ['store']. 编辑:我删除了地点搜索请求的类型参数,但即使使用参数['store'],该代码也无法使用。

var map;
var infowindow;


function initialize() {

var view_lat = document.getElementById('view_lat').value;
var view_lng = document.getElementById('view_lng').value;
var focus = new google.maps.LatLng(view_lat,view_lng);
var swlat = document.getElementById('swlat').value;
var swlng = document.getElementById('swlng').value;
var nelat = document.getElementById('nelat').value;
var nelng = document.getElementById('nelng').value;
var map_canvas = document.getElementById('map-canvas_embed');
var map_options = {
    center: new google.maps.LatLng(parseFloat(view_lat), view_lng),
    zoom: 13,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}
var mysw = new google.maps.LatLng(swlat,swlng)

var map = new google.maps.Map(map_canvas, map_options)
var rectangle = new google.maps.Rectangle({
    strokeColor: '#FF0000',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#FF0000',
    fillOpacity: 0.35,
    map: map,
    bounds: new google.maps.LatLngBounds(
        new google.maps.LatLng(swlat, swlng),
        new google.maps.LatLng(nelat, nelng))
});
var request = {
    location: focus,
    radius: 500

};

var place_search = new google.maps.places.PlacesService(map);
place_search.nearbySearch(request,callback)



}

Handles the result from google places search 处理来自Google地方搜索的结果

function callback(results, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < results.length; i++) {

      createMarker(results[i]);
    }
  }
}

function createMarker(place) {
var placeLoc = place.geometry.location;

var marker = new google.maps.Marker({

    position: place.geometry.location
});
marker.setMap(map);
}

Autocomplete variables 自动完成变量

var input = document.getElementById('location');
var options = {
    componentRestrictions: {country: 'se'},
    types: ['geocode']
}
var searchform = document.getElementById('searchform');
var place;
var autocomplete = new google.maps.places.Autocomplete(input, options);

Add listener to detect autocomplete selection 添加侦听器以检测自动完成选择

google.maps.event.addListener(autocomplete, 'place_changed', function () {
    place = autocomplete.getPlace();
    //console.log(place);
});

Add listener to search (This function isn't working, hence my work around in the initialize function) 将侦听器添加到搜索中(此功能不起作用,因此我在initialize函数中的工作方式)

searchform.addEventListener("submit", function() {
    var newlatlong = new       google.maps.LatLng(place.geometry.location.lat(),place.geometry.location.lng());
    map.setCenter(newlatlong);
    marker.setPosition(newlatlong);
    map.setZoom(12);
    google.maps.event.trigger(map, 'resize');

});

Reset the inpout box on click 重置点击输入框

input.addEventListener('click', function(){
    input.value = "";
});

Call the initialize function 调用初始化函数

google.maps.event.addDomListener(window, 'load', initialize);

Indeed it looks like a problem of JavaScript's scope. 确实,这看起来像是JavaScript范围的问题。

Consider these lines of your code: 考虑以下代码行:

    function initialize() {
      //...
      var map = new google.maps.Map(map_canvas, map_options)
      //...
     }

Declaring map inside the initialize function makes your map variable unreachable out of your initialize function. initialize函数内部声明map会使map变量无法通过initialize函数访问。

So when you try to reach it in your searchform , map.setCenter() will show in your console that map is undefined. 因此,当您尝试在searchform到达它时, map.setCenter()将在控制台中map未定义的map

You could solve your issue in several ways. 您可以通过多种方式解决您的问题。

  1. declare map at the beginning of your script (before initialize is executed, anyways) 在脚本的开头声明map (无论如何在执行initialize之前)
  2. declare the searchform.addEventListener("submit", function(){}) inside your initialize() function and use it before initialize() ends its execution. initialize()函数中声明searchform.addEventListener("submit", function(){}) ,并在initialize()结束执行之前使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM