2014-12-19 5 views
0

위치를 자동 완성하는 입력 상자를 두 개 만들고 제출 버튼을 클릭하면지도의 두 입력 위치간에 폴리 라인이 만들어집니다. 내 코드는 자동 완성을 완벽하게 수행하지만 제출 버튼을 클릭하면 사이트간에 선을 표시 할 수 없습니다. 제발 도와주세요. 디버깅에 도와주세요. 다음과 같이Google지도 API 맞춤형 폴리선

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script> 

<script> 
function initialize() { 
    var mapOptions = { 
center: new google.maps.LatLng(-33.8688, 151.2195), 
zoom: 13 
}; 
    var map = new google.maps.Map(document.getElementById('map-canvas'), 


mapOptions); 

    var input = /** @type {HTMLInputElement} */(
     document.getElementById('pac-input')); 

    var input1=(document.getElementById('pac-input1')); 

    //var types = document.getElementById('type-selector'); 
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input); 
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input1); 

    var autocomplete = new google.maps.places.Autocomplete(input); 
    autocomplete.bindTo('bounds', map); 

    var autocomplete = new google.maps.places.Autocomplete(input1); 
    autocomplete.bindTo('bounds', map); 

    var infowindow = new google.maps.InfoWindow(); 
    var marker = new google.maps.Marker({ 
    map: map, 
    anchorPoint: new google.maps.Point(0, -29) 
    }); 

    var polyOptions = { 
    strokeColor: '#000000', 
    strokeOpacity: 1.0, 
    strokeWeight: 3 
    }; 
    poly = new google.maps.Polyline(polyOptions); 
    poly.setMap(map); 
google.maps.event.addListener(submit, 'click', addLatLng); 


/** 
* Handles click events on a map, and adds a new point to the Polyline. 
* @param {google.maps.MouseEvent} event 
*/ 
function addLatLng(event) { 

    var path = poly.getPath(); 

    // Because path is an MVCArray, we can simply append a new coordinate 
    // and it will automatically appear. 
    path.push(event.latLng); 

    // Add a new marker at the new plotted point on the polyline. 
    var marker = new google.maps.Marker({ 
    position: event.latLng, 
    title: '#' + path.getLength(), 
    map: map 
    }); 
} 


    google.maps.event.addListener(autocomplete, 'place_changed', function() { 
    infowindow.close(); 
    marker.setVisible(false); 
    var place = autocomplete.getPlace(); 
    if (!place.geometry) { 
     return; 
    } 

    // If the place has a geometry, then present it on a map. 
    if (place.geometry.viewport) { 
     map.fitBounds(place.geometry.viewport); 
    } else { 
     map.setCenter(place.geometry.location); 
     map.setZoom(17); // Why 17? Because it looks good. 
    } 
    marker.setIcon(/** @type {google.maps.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(35, 35) 
    })); 
    marker.setPosition(place.geometry.location); 
    marker.setVisible(true); 

    var address = ''; 
    if (place.address_components) { 
     address = [ 
     (place.address_components[0] && place.address_components[0].short_name || ''), 
     (place.address_components[1] && place.address_components[1].short_name || ''), 
     (place.address_components[2] && place.address_components[2].short_name || '') 
     ].join(' '); 
    } 

    infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address); 
    infowindow.open(map, marker); 
    }); 


} 

google.maps.event.addDomListener(window, 'load', initialize); 

</script> 

내 HTML 파일의 본문은 다음과 같습니다 Uncaught ReferenceError: submit is not defined

google.maps.event.addListener(submit, 'click', addLatLng); 

가 있어야한다 :

google.maps.event.addListener(document.getElementById('submit'), 'click', addLatLng); 
<input id="pac-input" class="controls" type="text" placeholder="Enter a location"> 
<input id="pac-input1" class="controls" type="text" placeholder="Enter another location"> 

<div id="type-selector" class="controls"> 
    <input type="submit" name="submit" value="SUBMIT"> 
</div> 
<div id="map-canvas"></div> 
+0

나는 그것을 적합하게 만들기위한 질문. 보류 상태에서 제거하십시오. –

답변

1

내가 코드와 자바 스크립트 오류

(하지만 당신은 그럴 필요가 없습니다)

일단 수정하면 마커 하나만 나타납니다. 당신은 하나 개의 자동 완성 기능을 가지고 있기 때문에 :

var autocomplete = new google.maps.places.Autocomplete(input); 
autocomplete.bindTo('bounds', map); 

var autocomplete = new google.maps.places.Autocomplete(input1); 
autocomplete.bindTo('bounds', map); 

은 다음과 같아야합니다

var autocomplete = new google.maps.places.Autocomplete(input); 
autocomplete.bindTo('bounds', map); 

var autocomplete1 = new google.maps.places.Autocomplete(input1); 
autocomplete.bindTo('bounds', map); 

은 다음 autocomplete1위한 'place_changed'청취자가 필요합니다. 당신이 당신의 폴리 라인의 끝으로 그 위치를 원하는 경우

, 당신은 경로에 추가해야합니다 :

poly.getPath().setAt(0, marker.getPosition()); 

working fiddle

작업 코드 :

function initialize() { 
 
    var mapOptions = { 
 
     center: new google.maps.LatLng(-33.8688, 151.2195), 
 
     zoom: 13 
 
    }; 
 
    var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
 

 
    var input = /** @type {HTMLInputElement} */ 
 
    (
 
    document.getElementById('pac-input')); 
 

 
    var input1 = (document.getElementById('pac-input1')); 
 

 
    //var types = document.getElementById('type-selector'); 
 
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input); 
 
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input1); 
 

 
    var autocomplete = new google.maps.places.Autocomplete(input); 
 
    autocomplete.bindTo('bounds', map); 
 

 
    var autocomplete1 = new google.maps.places.Autocomplete(input1); 
 
    autocomplete.bindTo('bounds', map); 
 

 
    var infowindow = new google.maps.InfoWindow(); 
 
    var marker = new google.maps.Marker({ 
 
     map: map, 
 
     anchorPoint: new google.maps.Point(0, -29) 
 
    }); 
 

 
    var marker1 = new google.maps.Marker({ 
 
     map: map, 
 
     anchorPoint: new google.maps.Point(0, -29) 
 
    }); 
 
    var polyOptions = { 
 
     strokeColor: '#000000', 
 
     strokeOpacity: 1.0, 
 
     strokeWeight: 3 
 
    }; 
 
    poly = new google.maps.Polyline(polyOptions); 
 
    poly.setMap(map); 
 

 
    google.maps.event.addListener(autocomplete, 'place_changed', function() { 
 
     infowindow.close(); 
 
     marker.setVisible(false); 
 
     var place = autocomplete.getPlace(); 
 
     if (!place.geometry) { 
 
      return; 
 
     } 
 

 
     // If the place has a geometry, then present it on a map. 
 
     if (place.geometry.viewport) { 
 
      map.fitBounds(place.geometry.viewport); 
 
     } else { 
 
      map.setCenter(place.geometry.location); 
 
      map.setZoom(17); // Why 17? Because it looks good. 
 
     } 
 
     marker.setIcon(/** @type {google.maps.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(35, 35) 
 
     })); 
 
     marker.setPosition(place.geometry.location); 
 
     marker.setVisible(true); 
 
     poly.getPath().setAt(0, marker.getPosition()); 
 

 
     var address = ''; 
 
     if (place.address_components) { 
 
      address = [ 
 
      (place.address_components[0] && place.address_components[0].short_name || ''), (place.address_components[1] && place.address_components[1].short_name || ''), (place.address_components[2] && place.address_components[2].short_name || '')].join(' '); 
 
     } 
 

 
     infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address); 
 
     infowindow.open(map, marker); 
 
    }); 
 

 
    google.maps.event.addListener(autocomplete1, 'place_changed', function() { 
 
     infowindow.close(); 
 
     marker1.setVisible(false); 
 
     var place = autocomplete1.getPlace(); 
 
     if (!place.geometry) { 
 
      return; 
 
     } 
 

 
     // If the place has a geometry, then present it on a map. 
 
     if (place.geometry.viewport) { 
 
      map.fitBounds(place.geometry.viewport); 
 
     } else { 
 
      map.setCenter(place.geometry.location); 
 
      map.setZoom(17); // Why 17? Because it looks good. 
 
     } 
 
     marker1.setIcon(/** @type {google.maps.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(35, 35) 
 
     })); 
 
     marker1.setPosition(place.geometry.location); 
 
     marker1.setVisible(true); 
 
     poly.getPath().setAt(1, marker1.getPosition()); 
 

 
     var address = ''; 
 
     if (place.address_components) { 
 
      address = [ 
 
      (place.address_components[0] && place.address_components[0].short_name || ''), (place.address_components[1] && place.address_components[1].short_name || ''), (place.address_components[2] && place.address_components[2].short_name || '')].join(' '); 
 
     } 
 

 
     infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address); 
 
     infowindow.open(map, marker1); 
 
    }); 
 
} 
 

 
google.maps.event.addDomListener(window, 'load', initialize);
html, body, #map-canvas { 
 
    width: 100%; 
 
    height: 100%; 
 
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script> 
 
<input id="pac-input" class="controls" type="text" placeholder="Enter a location" /> 
 
<input id="pac-input1" class="controls" type="text" placeholder="Enter another location" /> 
 
<div id="type-selector" class="controls"> 
 
    <input type="submit" name="submit" id="submit" value="SUBMIT" /> 
 
</div> 
 
<div id="map-canvas"></div>

+0

답변을 주셔서 고마워요. 제출 버튼을 클릭하면 폴리선이 생깁니 까? 나는 너무 너무 이벤트를 만드는 방법에 대해 잘 모르겠습니다. –

+0

페이지에 있습니까? 코드 스 니펫과 바이올린에서 않습니다. 그렇다면 변경 내용을 놓치고 해당 요소에'id = "submit"을 추가했을 것입니다. – geocodezip

+0

아니요, 두 번째 위치를 입력하자마자 선을 만들고 제출 버튼을 클릭해도 아무런 변화가 없습니다. 제출 버튼을 누른 후 라인이 필요합니다. –