【问题标题】:Google maps API containslocation on a search change icon colorGoogle 地图 API 包含搜索更改图标颜色的位置
【发布时间】:2020-11-03 23:45:38
【问题描述】:

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

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.net/mLLepk9q
  • 请注意,您应该将代码发布在问题本身中,而不仅仅是指向小提琴或其他外部网站的链接。来自how to ask a good question:“如果可以创建一个您可以链接到的问题的实时示例(例如,在 sqlfiddle.com 或 jsbin.com 上),那么就这样做 - 但也要在您的问题本身中包含代码。不是每个人都可以访问外部网站,而且链接可能会随着时间的推移而中断。”。为您添加了代码。
  • 感谢您为我更新 geocodezip

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


【解决方案1】:
  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

工作代码sn-p:

// 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>

【讨论】:

    【解决方案2】:

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

    1. 在您的三角形多边形上设置 ma​​p 属性,以便开始显示它。

    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. 至于搜索结果 - 一旦结果返回,根据需要根据位置更改标记/颜色。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-04
      • 2011-01-29
      相关资源
      最近更新 更多