【问题标题】:Have Google Maps Places Search through markers in my own data让 Google Maps Places 搜索我自己数据中的标记
【发布时间】:2017-09-10 02:53:37
【问题描述】:

我有一个 JSON 数组,其中包含名称、纬度和经度等属性。我会将它们作为标记放在地图上。然后,我想使用 Google 的自动完成地点搜索来搜索我的数组中的“名称”字段并显示与搜索匹配的任何标记。这可能吗?

 //global variables
var gmarkers1 = [];
var markers1 = [];
var infowindow = new google.maps.InfoWindow({
    content: ''
});

//add the shipwreck data
markers1 = [
  ["Name","Latitude","Longitude","Year","Type","Type_of_Loss"  ],
  ["Bermuda","46.46483333","-86.64683333","1870","Schooner","Storm"  ],
  ["George","46.516","-86.52083333","1893","Schooner","Storm"  ],
  ["Herman H. Hettler","46.48383333","-86.59966667","1926","Propeller","Storm"  ],
  ["Kiowa","46.64516667","-86.22016667","1929","Propeller","Storm"  ],
  ["Manhattan","46.467","-86.60933333","1903","Propeller","Storm"  ],

];


//initiate the map
function initMap() {
    // Our markers


    var center = new google.maps.LatLng(45.2858536, -83.8419731);
    var mapOptions = {
        zoom: 6,
        center: center,
        mapTypeId: google.maps.MapTypeId.TERRAIN
    };



    map = new google.maps.Map(document.getElementById('map'), mapOptions);
    for (i = 0; i < markers1.length; i++) {
        addMarker(markers1[i]);
    }
  // map.data.loadGeoJson('data/lakebath.geojson');
//    map.data.setStyle({
//  fillColor: 'green',
//  strokeWeight: 1,
//  strokeOpacity: 0.5        
//});
}


//add markers to the map
function addMarker(marker) {    
    var category_type = marker[4];
    var category_loss = marker[5];
    var category_year = marker[0];
    var title = marker[0];
    var pos = new google.maps.LatLng(marker[1], marker[2]);
    var content = marker[0];

    marker1 = new google.maps.Marker({
        title: title,
        position: pos,
        //setup the categories for the different mark types
        category_type: category_type,
        category_loss: category_loss,
        category_year: category_year,
        map: map
    });

【问题讨论】:

    标签: javascript algorithm


    【解决方案1】:

    我不相信这是可能的 - Google Places API 可让您搜索 Google 的地点,但不能搜索本地维护的一组地点。

    我不会尝试使用 Places API,而是在本地搜索您的 markers1 数组,看看搜索词是否是标记名称的 substring

    【讨论】:

    猜你喜欢
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    • 2016-02-08
    • 2012-09-27
    • 2016-06-17
    • 2015-03-15
    • 2012-02-26
    相关资源
    最近更新 更多