谷歌地图:通过几个输入字段设置标记

时间:2015-01-30 11:47:55

标签: google-maps-api-3

如何根据地址输入更改标记的位置?我知道谷歌的地方有自动完成工作,但要求是在填写地址表后更新地图标记。

JS Fiddle可以在这里找到: http://jsfiddle.net/fhft7bfg/2/

HTML:

<input id="route" placeholder="Street"></input>
<input id="locality" placeholder="City"></input>
<input id="administrative_area_level_1" placeholder="State"></input>
<input id="postal_code" placeholder="Postal Code"></input>
<input id="country" placeholder="Country"></input>
<div id="map-canvas"></div>

JS:

function initialize() {
var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var mapOptions = {
  zoom: 4,
  center: myLatlng
}
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

// To add the marker to the map, use the 'map' property
var marker = new google.maps.Marker({
    position: myLatlng,
    map: map,
    title:"Hello World!"
});
}
google.maps.event.addDomListener(window, 'load', initialize);

1 个答案:

答案 0 :(得分:1)

  1. 制作地图和标记全局
  2. 修改Google's simple geocoder example中的codeAddress功能以使用表单元素,并移动标记(如果已存在)。
  3. working fiddle

    工作代码段:

    // modified from https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple
    var geocoder = new google.maps.Geocoder();
    function codeAddress() {
        var street = document.getElementById("route").value;
        var city = document.getElementById("locality").value;
        var state = document.getElementById("administrative_area_level_1").value;
        var postcode = document.getElementById("postal_code").value;
        var country = document.getElementById("country").value;
    var address = street +"," + city + "," + state +"," + postcode + "," + country;    
        geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          map.setCenter(results[0].geometry.location);
            if (marker && marker.setPosition) {
                marker.setPosition(results[0].geometry.location);
            } else {
          marker = new google.maps.Marker({
              map: map,
              position: results[0].geometry.location
          });
            }
        } else {
          alert('Geocode was not successful for the following reason: ' + status);
        }
      });
    }
    
    // global variables
    var marker;
    var map;
    function initialize() {
        var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
        var mapOptions = {
          zoom: 4,
          center: myLatlng
        }
        map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
        
        // To add the marker to the map, use the 'map' property
        marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title:"Hello World!"
        });
    }
    
    google.maps.event.addDomListener(window, 'load', initialize);
    input{display:block}
    
    #map-canvas{background:#ccc; width:500px; height:300px}
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <input id="route" placeholder="Street" value="Beacon St"></input>
    
    <input id="locality" placeholder="City" value="Boston"></input>
    
    <input id="administrative_area_level_1" placeholder="State" value="MA"></input>
    
    <input id="postal_code" placeholder="Postal Code" value="02215"></input>
    
    <input id="country" placeholder="Country" value="US"></input>
    <input id="geocode" type="button" onclick="codeAddress();" value="geocode"/>
    <div id="map-canvas"></div>