Google maps API containslocation on a search 更改图标颜色

Google maps API containslocation on a search change icon color

我正在尝试让我的 google 地图根据结果所在的位置更改搜索图标的颜色。

API info for places-searchbox

API for poly-containsLocation

我正在尝试将这些结合起来,以便搜索到的位置的图标会根据结果的位置发生变化。 第二部分是让图标在拖入或拖出多边形时改变颜色。

而不是在点击地图时改变它 我想在生成时更改搜索图标的颜色... 只是不确定我的代码会去哪里...像这样:

// This example adds a search box to a map, using the Google Place Autocomplete
// feature. People can enter geographical searches. The search box will return a
// pick list containing a mix of places and predicted search terms.

function initialize() {

  var markers = [];
  var map = new google.maps.Map(document.getElementById('map-canvas'), {
    mapTypeId: google.maps.MapTypeId.ROADMAP
  });
     var triangleCoords = [
    new google.maps.LatLng(-33.8902, 151.1759),
    new google.maps.LatLng(-33.8474, 151.2631),
    new google.maps.LatLng(-33.8902, 151.2631)
  ];
  var theTriangle = new google.maps.Polygon({
    paths: triangleCoords
  });
google.maps.event.addListener(map, 'click', function(e) {
    var result;
    if (google.maps.geometry.poly.containsLocation(e.latLng, theTriangle)) {
      result = 'red';
    } else {
      result = 'green';
    }

    var circle = {
      path: google.maps.SymbolPath.CIRCLE,
      fillColor: result,
      fillOpacity: .2,
      strokeColor: 'white',
      strokeWeight: .5,
      scale: 10
    };

    new google.maps.Marker({
      position: e.latLng,
      map: map,
      icon: circle
    })
  });


  var defaultBounds = new google.maps.LatLngBounds(
      new google.maps.LatLng(-33.8902, 151.1759),
      new google.maps.LatLng(-33.8474, 151.2631));
  map.fitBounds(defaultBounds);

  // Create the search box and link it to the UI element.
  var input = /** @type {HTMLInputElement} */(
      document.getElementById('pac-input'));
  map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

  var searchBox = new google.maps.places.SearchBox(
    /** @type {HTMLInputElement} */(input));

  // [START region_getplaces]
  // Listen for the event fired when the user selects an item from the
  // pick list. Retrieve the matching places for that item.
  google.maps.event.addListener(searchBox, 'places_changed', function() {
    var places = searchBox.getPlaces();

    if (places.length == 0) {
      return;
    }
    for (var i = 0, marker; marker = markers[i]; i++) {
      marker.setMap(null);
    }

    // For each place, get the icon, place name, and location.
    markers = [];
    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)
      };

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

      markers.push(marker);

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

    map.fitBounds(bounds);
  });
  // [END region_getplaces]

  // Bias the SearchBox results towards places that are within the bounds of the
  // current map's viewport.
  google.maps.event.addListener(map, 'bounds_changed', function() {
    var bounds = map.getBounds();
    searchBox.setBounds(bounds);
  });
}

google.maps.event.addDomListener(window, 'load', initialize);
html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
      .controls {
        margin-top: 16px;
        border: 1px solid transparent;
        border-radius: 2px 0 0 2px;
        box-sizing: border-box;
        -moz-box-sizing: border-box;
        height: 32px;
        outline: none;
        box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
      }

      #pac-input {
        background-color: #fff;
        padding: 0 11px 0 13px;
        width: 400px;
        font-family: Roboto;
        font-size: 15px;
        font-weight: 300;
        text-overflow: ellipsis;
      }

      #pac-input:focus {
        border-color: #4d90fe;
        margin-left: -1px;
        padding-left: 14px;  /* Regular padding-left + 1. */
        width: 401px;
      }

      .pac-container {
        font-family: Roboto;
      }

      #type-selector {
        color: #fff;
        background-color: #4d90fe;
        padding: 5px 11px 0px 11px;
      }

      #type-selector label {
        font-family: Roboto;
        font-size: 13px;
        font-weight: 300;
      }
}
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places"></script>
<input id="pac-input" class="controls" type="text" placeholder="Search Box">
<div id="map-canvas"></div>

你的 JSFiddle 很乱。所以我试着解释一下:

  1. 在您的三角形多边形上设置 地图 属性 以显示它作为开始。

  2. 将标记放入多边形时更改标记颜色: 你的标记可拖动看这个例子: https://developers.google.com/maps/documentation/javascript/examples/marker-animations

    添加一个 dragend 事件到你的标记何时触发 - 检查是否 它在多边形内:

    google.maps.event.addListener(Marker, "dragend", function(event) {
     // check if marker is in polygon and change color or marker
    }); 
    
  3. 至于搜索结果 - 一旦结果返回,根据需要根据位置更改 marker/color。

  1. 添加几何库
<script src="https://maps.googleapis.com/maps/api/js?libraries=places,geometry"></script>
  1. 检查每个标记是否在多边形中,如果是则更改其图标:
var alternateIcon = "http://maps.google.com/mapfiles/ms/icons/blue.png";
if (google.maps.geometry.poly.containsLocation(marker.getPosition(), theTriangle)) 
  marker.setIcon(alternateIcon);

working fiddle

工作代码片段:

// This example adds a search box to a map, using the Google Place Autocomplete
// feature. People can enter geographical searches. The search box will return a
// pick list containing a mix of places and predicted search terms.
var theTriangle;
var map;

function initialize() {

  var markers = [];
  map = new google.maps.Map(document.getElementById('map-canvas'), {
    mapTypeId: google.maps.MapTypeId.ROADMAP
  });
  var triangleCoords = [
    new google.maps.LatLng(-33.8902, 151.1759),
    new google.maps.LatLng(-33.8474, 151.2631),
    new google.maps.LatLng(-33.8902, 151.2631)
  ];
  theTriangle = new google.maps.Polygon({
    paths: triangleCoords,
    map: map,
    clickable: false
  });
  google.maps.event.addListener(map, 'click', function(e) {
    var result;
    if (google.maps.geometry.poly.containsLocation(e.latLng, theTriangle)) {
      result = 'red';
    } else {
      result = 'green';
    }

    var circle = {
      path: google.maps.SymbolPath.CIRCLE,
      fillColor: result,
      fillOpacity: .2,
      strokeColor: 'white',
      strokeWeight: .5,
      scale: 10
    };

    new google.maps.Marker({
      position: e.latLng,
      map: map,
      icon: circle
    })
  });


  var defaultBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(-33.8902, 151.1759),
    new google.maps.LatLng(-33.8474, 151.2631));
  map.fitBounds(defaultBounds);

  // Create the search box and link it to the UI element.
  var input = /** @type {HTMLInputElement} */
    (
      document.getElementById('pac-input'));
  map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

  var searchBox = new google.maps.places.SearchBox(
    /** @type {HTMLInputElement} */
    (input));

  // [START region_getplaces]
  // Listen for the event fired when the user selects an item from the
  // pick list. Retrieve the matching places for that item.
  google.maps.event.addListener(searchBox, 'places_changed', function() {
    var places = searchBox.getPlaces();

    if (places.length == 0) {
      return;
    }
    for (var i = 0, marker; marker = markers[i]; i++) {
      marker.setMap(null);
    }

    // For each place, get the icon, place name, and location.
    markers = [];
    var bounds = new google.maps.LatLngBounds();
    var alternateIcon = "http://maps.google.com/mapfiles/ms/icons/blue.png";
    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)
      };

      // Create a marker for each place.

      var marker = new google.maps.Marker({
        map: map,
        icon: image,
        title: place.name,
        position: place.geometry.location
      });
      if (google.maps.geometry.poly.containsLocation(marker.getPosition(), theTriangle)) marker.setIcon(alternateIcon);

      markers.push(marker);

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

    map.fitBounds(bounds);
  });
  // [END region_getplaces]

  // Bias the SearchBox results towards places that are within the bounds of the
  // current map's viewport.
  google.maps.event.addListener(map, 'bounds_changed', function() {
    var bounds = map.getBounds();
    searchBox.setBounds(bounds);
  });
}

google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map-canvas {
  height: 100%;
  margin: 0px;
  padding: 0px
}
.controls {
  margin-top: 16px;
  border: 1px solid transparent;
  border-radius: 2px 0 0 2px;
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  height: 32px;
  outline: none;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}
#pac-input {
  background-color: #fff;
  padding: 0 11px 0 13px;
  width: 400px;
  font-family: Roboto;
  font-size: 15px;
  font-weight: 300;
  text-overflow: ellipsis;
}
#pac-input:focus {
  border-color: #4d90fe;
  margin-left: -1px;
  padding-left: 14px;
  /* Regular padding-left + 1. */
  width: 401px;
}
.pac-container {
  font-family: Roboto;
}
#type-selector {
  color: #fff;
  background-color: #4d90fe;
  padding: 5px 11px 0px 11px;
}
#type-selector label {
  font-family: Roboto;
  font-size: 13px;
  font-weight: 300;
}
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=places,geometry&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<input id="pac-input" class="controls" type="text" placeholder="Search Box">
<div id="map-canvas"></div>