2

我正在尝试使用 Google Places API。我从https://developers.google.com/maps/documentation/javascript/examples/place-search-pagination获得了这些代码。

var map, placesList;

function initialize() {
  var pyrmont = new google.maps.LatLng(-33.8665433, 151.1956316);

  map = new google.maps.Map(document.getElementById('map-canvas'), {
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: pyrmont,
    zoom: 17
  });

  var request = {
    location: pyrmont,
    radius: 500,
    types: ['store']
  };

  placesList = document.getElementById('places');

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

function callback(results, status, pagination) {
  if (status != google.maps.places.PlacesServiceStatus.OK) {
    return;
  } else {
    createMarkers(results);

    if (pagination.hasNextPage) {
      var moreButton = document.getElementById('more');

      moreButton.disabled = false;

      google.maps.event.addDomListenerOnce(moreButton, 'click',
          function() {
        moreButton.disabled = true;
        pagination.nextPage();
      });
    }
  }
}

function createMarkers(places) {
  var bounds = new google.maps.LatLngBounds();

  for (var i = 0, place; place = places[i]; i++) {
    var image = {
      url: place.icon,
      size: new google.maps.Size(71, 71),
      origin: new google.maps.Point(0, 0),
      anchor: new google.maps.Point(17, 34),
      scaledSize: new google.maps.Size(25, 25)
    };

    var marker = new google.maps.Marker({
      map: map,
      icon: image,
      title: place.name,
      position: place.geometry.location
    });

    placesList.innerHTML += '<li>' + place.name + '</li>';

    bounds.extend(place.geometry.location);
  }
  map.fitBounds(bounds);
}

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

使用这些代码,我从位置获取名称,并将其更改为类型。现在我得到了一个类型列表,但它们之间有逗号。

    placesList.innerHTML += '<li>' + place.types + '</li>';

如何删除逗号?例如,它们看起来像这样:

<li>accounting,airport,amusement_park</li>
<li>aquarium,art_gallery,atm,bakery</li>
<li>bank,bar,beauty_salon</li>

我想用空格替换逗号。有人能帮我吗?

4

1 回答 1

1

利用join

place.types.join(" ")
于 2013-06-22T22:00:09.370 回答