Highlight a area with Google Maps JavaScript API v3

后端 未结 2 1767
小蘑菇
小蘑菇 2021-01-15 17:31

I want to highlight a area like on the image below which is taken from Google Maps. Is this possible to accomplish with the current version of their API (v3)? If yes, how?

相关标签:
2条回答
  • 2021-01-15 18:16

    You can automatically highlight an area with Google Maps Javascript API, here is an example:

    <!DOCTYPE html>
    <html>
      <head>
        <title>Place ID Finder</title>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
        <meta charset="utf-8">
        <style>
          #map {
            height: 100%;
          }
    
          html, body {
            height: 100%;
            margin: 0;
            padding: 0;
          }
    
          #ggg
          {
              position: absolute;
              left:20%;
              top:5%;
          }
    
          .controls {
            background-color: #fff;
            border-radius: 2px;
            border: 1px solid transparent;
            box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
            box-sizing: border-box;
            font-family: Roboto;
            font-size: 15px;
            font-weight: 300;
            height: 29px;
            margin-left: 17px;
            margin-top: 10px;
            outline: none;
            padding: 0 11px 0 13px;
            text-overflow: ellipsis;
            width: 400px;
          }
    
          .controls:focus {
            border-color: #4d90fe;
          }
          .title {
            font-weight: bold;
          }
          #infowindow-content {
            display: none;
          }
          #map #infowindow-content {
            display: inline;
          }
    
        </style>
      </head>
      <body>
        <div style="display: none">
            <input id="pac-input" class="controls" type="text" placeholder="Enter a location">
        </div>
        <div id="map"></div>
        <div id="infowindow-content">
            <span id="place-name" class="title"></span><br>
            <strong>Place ID:</strong> <span id="place-id"></span><br>
            <span id="place-address"></span>
        </div>
    
        <script>
          function initMap() {
            var map = new google.maps.Map(document.getElementById('map'), {
              center: {lat: 37.9989746, lng: 23.6413698},
              zoom: 11
            });
    
            var input = document.getElementById('pac-input');
    
            var autocomplete = new google.maps.places.Autocomplete(input);
            autocomplete.bindTo('bounds', map);
    
            autocomplete.setFields(
                ['place_id', 'geometry', 'name', 'formatted_address']);
    
            map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
    
            var infowindow = new google.maps.InfoWindow();
            var infowindowContent = document.getElementById('infowindow-content');
            infowindow.setContent(infowindowContent);
    
            var marker = new google.maps.Marker({map: map});
    
            marker.addListener('click', function() {
              infowindow.open(map, marker);
            });
    
            autocomplete.addListener('place_changed', function() {
              infowindow.close();
    
              var place = autocomplete.getPlace();
    
              if (!place.geometry) {
                return;
              }
    
              if (place.geometry.viewport) {
                map.fitBounds(place.geometry.viewport);
              } else {
                map.setCenter(place.geometry.location);
                map.setZoom(17);
              }
    
              marker.setPlace({
                placeId: place.place_id,
                location: place.geometry.location,
              });
    
              marker.setVisible(true);
    
              infowindowContent.children['place-name'].textContent = place.name;
              infowindowContent.children['place-id'].textContent = place.place_id;
              infowindowContent.children['place-address'].textContent = place.formatted_address;
              infowindow.open(map, marker);
    
               var frame = document.getElementById("map");
    
               frame.innerHTML = '<iframe width="100%" height="100%" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/place?q=place_id:'.concat(place.place_id).concat('&key=XXXXXXX" allowfullscreen></iframe>');
    
               frame.innerHTML += '<p id="ggg"><button type="button" onclick="location.reload()">Try again!</button></p>';
            });
          }
    
        </script>
        <script src="https://maps.googleapis.com/maps/api/js?key=XXXXXXXXX&libraries=places&callback=initMap" async defer></script>
    
    </body>
    </html>
    

    You will need to replace API KEY with your API KEY. Try e.g."Nea Smyrni 171 21, Greece" in the input and then select the first drop down option and notice that it automatically highlights the area!

    0 讨论(0)
  • 2021-01-15 18:27

    You need to know the vertices of the area and create a polygon based on them.

    But dashed strokes currently are not supported by polygons, if you require to have a dashed stroke you must create a sequence of polylines with different stroke-colors based on the vertices.

    A built-in method to highlight an area currently doesn't exist.

    0 讨论(0)
提交回复
热议问题