在地图下添加标记的UL

时间:2015-08-11 17:49:45

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

尝试在地图下方的半径范围内添加地点标记列表。这样做的是新函数调用上面的回调或createMarker函数吗?

我的逻辑是使用将标记放在地图上的函数填充列表。

这是当前代码,我的列表函数的开头已注释掉:

  var geocoder;
var map;
var infowindow;
var webster = new google.maps.LatLng(43.200371, -77.498245);

function initialize() {  
  var latlng = new google.maps.LatLng(-34.397, 150.644);
  var mapOptions = {
    zoom: 8,
    center: latlng
  }
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
codeAddress();
var request = {
    location: webster,
    radius: 50000,
    types: ['store']
  };
  infowindow = new google.maps.InfoWindow();
  var service = new google.maps.places.PlacesService(map);
  service.nearbySearch(request, callback);
}

function codeAddress() {
  geocoder = new google.maps.Geocoder();  
  var address = '2450 Empire Blvd, Webster, NY 14580 US';   
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
      });
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}

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({
    map: map,
    position: place.geometry.location
  });

function createUL(){
    var items = document.getElementById("results");

    for (var i = 0; i < results.length; i++ ) {
        var item = document.createElement("li");
        item.innerHTML = results[i];
        items.appendChild(item);
    }

}

  google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(place.name);
    infowindow.open(map, this);
  });
}


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

jsfiddle.net/michaelkurz_/jgm72khd/33/

1 个答案:

答案 0 :(得分:0)

  1. 调用createUL函数。
  2. 将places查询中的结果传递给它。
  3. 保存对标记的引用(我在gmarkers数组中完成)
  4. 将UL中的名称链接到标记
  5. proof of concept fiddle

    &#13;
    &#13;
    var geocoder;
    var map;
    var infowindow;
    var webster = new google.maps.LatLng(43.200371, -77.498245);
    var gmarkers = [];
    
    function initialize() {
      var latlng = new google.maps.LatLng(-34.397, 150.644);
      var mapOptions = {
        zoom: 8,
        center: latlng
      };
      map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
      codeAddress();
      var request = {
        location: webster,
        radius: 50000,
        types: ['store']
      };
      infowindow = new google.maps.InfoWindow();
      var service = new google.maps.places.PlacesService(map);
      service.nearbySearch(request, callback);
    }
    
    function codeAddress() {
      geocoder = new google.maps.Geocoder();
      var address = '2450 Empire Blvd, Webster, NY 14580 US';
      geocoder.geocode({
        'address': address
      }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          map.setCenter(results[0].geometry.location);
          var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
          });
        } else {
          alert('Geocode was not successful for the following reason: ' + status);
        }
      });
    }
    
    function callback(results, status) {
      if (status == google.maps.places.PlacesServiceStatus.OK) {
        for (var i = 0; i < results.length; i++) {
          createMarker(results[i]);
        }
        createUL(results);
      }
    }
    
    function createMarker(place) {
      var placeLoc = place.geometry.location;
      var marker = new google.maps.Marker({
        map: map,
        position: place.geometry.location
      });
    
    
    
      google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(place.name);
        infowindow.open(map, this);
      });
      gmarkers.push(marker);
    }
    
    function createUL(results) {
      var items = document.getElementById("results");
    
      for (var i = 0; i < results.length; i++) {
        var item = document.createElement("li");
        item.innerHTML = "<a href='javascript:google.maps.event.trigger(gmarkers[" + i + "],\"click\");'>" + results[i].name + "</a><br>";
        items.appendChild(item);
      }
    
    }
    
    google.maps.event.addDomListener(window, 'load', initialize);
    &#13;
    #map-canvas {
      width: 450px;
      height: 400px;
    }
    &#13;
    <script src="http://maps.googleapis.com/maps/api/js?libraries=places"></script>
    <div id="map-canvas"></div>
    <div id="results"></div>
    &#13;
    &#13;
    &#13;