将地点搜索和kml图层与Google地图上的切换结合起来

时间:2015-10-27 13:34:13

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

我正在尝试创建一个包含多个kml图层的地图,这些图层可以通过Google地方搜索进行切换,但一直未成功。这样做的目的是能够搜索地址或交叉点并确定用于调度中心的火灾或医疗呼叫的正确响应装置,其中搜索偏向正确的县​​。我的基本地图只有一个图层工作here,但我无法正确地将kml图层切换与Google地方搜索集成。我在使用地理编码函数geocodezip时收到了here的一些好的输入,但是我无法将其偏向正确的位置或包含预测搜索完成。我也喜欢我的原始地图将缩放并以搜索到的地址为中心并希望保持该功能的方式。不可否认,我对这方面的能力非常缺乏,我所做的一切都是简单地复制和修改其他人发布的工作示例,但我没有发现任何包含谷歌地方搜索和kml图层的工作示例切换。这就是我到目前为止所拥有的。任何帮助实现这一目标的人都将非常感激。



<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 100%;
      }
.controls {
  margin-top: 10px;
  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;
  font-family: Roboto;
  font-size: 15px;
  font-weight: 300;
  margin-left: 12px;
  padding: 0 11px 0 13px;
  text-overflow: ellipsis;
  width: 300px;
}
#pac-input:focus {
  border-color: #4d90fe;
}
.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;
}
    </style>
    <title>Advanced Backup Map</title>
    <style>
      #target {
        width: 345px;
      }
    </style>
    
    <script>
    
      var kml = {
    a: {
        name: "Fire Response Areas",
        url: "https://drive.google.com/uc?export=download&id=0B2gbIV1dXlvDVDhVLXc2N1Y5ZEE"
    },
    b: {
        name: "SD Counties",
        url: "https://drive.google.com/uc?export=download&id=0B2gbIV1dXlvDRndwdEpKTjRBeTA"
    },
    c: {
        name: "Counties in Surrounding States",
        url: "https://drive.google.com/uc?export=download&id=0B2gbIV1dXlvDRlhLTm93S2Y3eDQ"
    }
    // keep adding more if ye like 
  };
    
// 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 initAutocomplete() {
  // lets define some vars to make things easier later
  
  var options = {
    center: new google.maps.LatLng(43.64837, -96.73737),
    zoom: 10,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  
  var map = new google.maps.Map(document.getElementById('map'), options);
  createTogglers();
  
  // Append Class on Select
function highlight(box, listitem) {
    var selected = 'selected';
    var normal = 'normal';
    document.getElementById(listitem).className = (box.checked ? selected : normal);
};
function startup() {
    // for example, this toggles kml a on load and updates the menu selector
    var checkit = document.getElementById('a');
    checkit.checked = true;
    toggleKML(checkit, 'a');
    highlight(checkit, 'selector1');
};
  // Create the search box and link it to the UI element.
  var defaultBounds = new google.maps.LatLngBounds(
  new google.maps.LatLng(43.38218, -97.29373),
  new google.maps.LatLng(43.92451, -96.34532));
  
  var input = document.getElementById('pac-input');
  var searchBox = new google.maps.places.SearchBox(input, {
  bounds: defaultBounds
});
  map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
  // Bias the SearchBox results towards current map's viewport.
  map.addListener('bounds_changed', function() {
    searchBox.setBounds(map.getBounds());
  });
  var markers = [];
  // [START region_getplaces]
  // Listen for the event fired when the user selects a prediction and retrieve
  // more details for that place.
  searchBox.addListener('places_changed', function() {
    var places = searchBox.getPlaces();
    if (places.length == 0) {
      return;
    }
    // Clear out the old markers.
    markers.forEach(function(marker) {
      marker.setMap(null);
    });
    markers = [];
    // For each place, get the icon, name and location.
    var bounds = new google.maps.LatLngBounds();
    places.forEach(function(place) {
      var icon = {
        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.
      markers.push(new google.maps.Marker({
        map: map,
        icon: icon,
        title: place.name,
        position: place.geometry.location
      }));
      if (place.geometry.viewport) {
        // Only geocodes have viewport.
        bounds.union(place.geometry.viewport);
      } else {
        bounds.extend(place.geometry.location);
      }
    });
    map.fitBounds(bounds);
       map.setZoom(16);
   map.setCenter(center);
   });
  // [END region_getplaces]
// the important function... kml[id].xxxxx refers back to the top 
function toggleKML(checked, id) {
    if (checked) {
        var layer = new google.maps.KmlLayer(kml[id].url, {
            preserveViewport: true,
            suppressInfoWindows: false
        });
        // store kml as obj
        kml[id].obj = layer;
        kml[id].obj.setMap(map);
    }
    else {
        kml[id].obj.setMap(null);
        delete kml[id].obj;
    }
};
// create the controls dynamically because it's easier, really
function createTogglers() {
    var html = "<form><ul>";
    for (var prop in kml) {
        html += "<li id=\"selector-" + prop + "\"><input type='checkbox' id='" + prop + "'" +
        " onclick='highlight(this,\"selector-" + prop + "\"); toggleKML(this.checked, this.id)' \/>" +
        kml[prop].name + "<\/li>";
    }
    document.getElementById("toggle_box").innerHTML = html;
};
}
    </script>
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBhTQXonppCE2eVDQO5AHy11kMn-o27m_U&libraries=places&callback=initAutocomplete"
         async defer></script>
    
  </head>
  <body>
    <input id="pac-input" class="controls" type="text" placeholder="Search Box">
    <div id="map"></div>
    <div id="toggle_box" style="position: absolute; top: 10px; right: 10px; padding: 5px; background: #fff; z-index: 1; "></div>

  </body>
</html>
&#13;
&#13;
&#13;

0 个答案:

没有答案