2010-03-21 4 views
2

누군가 제발 나를 도와 줄 수 있습니까? 나는 내 코드가 무엇이 잘못되었는지 알지 못한다. 정보창에 위치 주소를 표시하고 싶지만, 매번 현재 위치 대신 이전 위치를 표시한다. 예를 들어 처음 위치를 클릭하면 마커가 추가되지만 표시되는 주소는 정의되지 않습니다 (이전 결과). 그럼 내가 다른 지점을 클릭하면 첫 번째 주소가 표시됩니다.Google지도 정보 창에 현재 결과 대신 이전 지역 결과가 표시됩니다.

어떻게 현재 표시 위치의 주소를 표시하도록이 문제를 해결할 수 있습니까? 부디. 고마워.

geocoder.geocode({'latLng': loc}, function(results, status){ 
          if (status == google.maps.GeocoderStatus.OK) { 
           if (results[0]) { addr = results[0].formatted_address; } 
          } 
          else { alert("No Matching Results"); } 
       }); 

I : 아래에있는 내 코드 ..

var map; 
var marker; 
var markersArray = []; 

var infoWindow; 
var buffer; 

var geocoder, regeocoder; 

function initialize() { 

window.onunload = google.maps.Unload; 

// Creating an option object for the map 

    var myOptions = { 
      zoom: 16, 
      center:COUNTRY, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 

// Initializing the map 

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

// Add onClick event to the map 

    google.maps.event.addListener(map, 'click', function(event) { placeMarker(event.latLng, true); }); 
} 

function placeMarker(location, flag) { 

// Get clicked location <Latitude, Longtitude> 

var clickedLocation = location; 

    if (markersArray) { 
     for (i in markersArray) { 
       markersArray[i].setMap(null); 
     } 
     markersArray.length = 0; 
    } 

    // Create a new marker 

    marker = new google.maps.Marker({ 
     position: clickedLocation, 
      map: map, 
      icon: 'image/blue-dot.png', 
      title: "Select this location", 
      clickable: true 
    }); 

    if (flag == true) { 

     // Start reverse Geocode 

     regeocoder = new google.maps.Geocoder(); 

     if (regeocoder) { 
       regeocoder.geocode({'latLng': clickedLocation, 'region': region}, function(results, status){ 

       if (status == google.maps.GeocoderStatus.OK) { 
       if (results[0]) { buffer = results[0].formatted_address; } 
       } 
      else { } 
       }); 
     } 

    setHiddenValue(buffer); 

    setMarkerInfo(buffer, clickedLocation); 

    } 

    // Attach mouseover event to a marker that will trigger the markerInfo 

    google.maps.event.addListener(marker, 'mouseover', function() { infowindow.open(map,marker); }); 

    // Attach mouseout event to the marker that will delete the markerInfo 

    google.maps.event.addListener(marker, 'mouseout', function() { if (infowindow) infowindow.close(); }); 

    markersArray.push(marker); 

    map.setCenter(clickedLocation); 
} 


function setMarkerInfo(title, textbody) { 

    // Initialize the contentString 

    var contentString = '<div id="content">'+'<div id="siteNotice">'+'</div>'+ 
    '<br/><h3 id="firstHeading" class="firstHeading">' + title + '</h1>'+ 
    '<div id="bodyContent">'+ 
    '<p>Map Coordinates: <br/>' + textbody + '</p>'+ 
    '</div>'+ 
    '</div>'; 

    infowindow = new google.maps.InfoWindow({ content: contentString }); 
    infosArray.push(infowindow); 
} 

function setHiddenValue(data) { 
    var hiddenVal = document.getElementById('getLoc'); 
    if (hiddenVal) { hiddenVal.value = data; } 
} 

function searchMap(info) { 

var address = info; 
var loc; 
var addr; 

    geocoder = new google.maps.Geocoder(); 

    if (geocoder) { 
      geocoder.geocode({'address': address, 'region': region}, function(sresults, sstatus){ 
       if (sstatus == google.maps.GeocoderStatus.OK) { 

        if (sresults[0]) { 

        loc = sresults[0].geometry.location; 

        geocoder.geocode({'latLng': loc}, function(results, status){ 
          if (status == google.maps.GeocoderStatus.OK) { 
          if (results[0]) { addr = results[0].formatted_address; } 
          } 
         else { alert("No Matching Results"); } 
         }); 

       } 

      }else { alert("No Matching Results"); } 

      }); 
    } 

    if (geocoder) { 
      geocoder.geocode({'address': address, 'region': region}, function(sresults, sstatus){ 
       if (sstatus == google.maps.GeocoderStatus.OK) { 

        if (sresults[0]) { 

        loc = sresults[0].geometry.location; 

        geocoder.geocode({'latLng': loc}, function(results, status){ 
          if (status == google.maps.GeocoderStatus.OK) { 
          if (results[0]) { addr = results[0].formatted_address; } 
          } 
         else { alert("No Matching Results"); } 
         }); 

       } 

      }else { alert("No Matching Results"); } 

      }); 
    } 

    setHiddenValue(addr); 

    setMarkerInfo(addr, loc); 

    placeMarker(loc, false); 

} 

답변

1

귀하의 문제는 주소 정보가 당신의 상대 형식의 주소를 얻기 전에 따라 모든 코드를 실행, 비동기 적으로 도착한다는 사실에 연결되어있다 결합 된 함수 호출에 모든 것을 연결하는 것이 좋습니다 :

google.maps.event.addListener(marker, 'mouseover', function() { 

과 대신을 :

,369을
infowindow.open(map,marker); }); 

당신은 전화 :

new google.maps.InfoWindow({ content: 

과 대신 :

geocoder.geocode({'latLng': loc}, function(results, status){ 
    if (status == google.maps.GeocoderStatus.OK) { 
     if (results[0]) { addr = results[0].formatted_address; } 
    } else { 
     * alert("No Matching Results"); */ 
    } 
}); 

을하고 여기 CSS의 형태로 포맷 된 결과를 얻을 :

contentString }); 

당신이 전화 너는 그것을 원한다. 문자열.

이렇게하면 마커를 건너 뛸 때마다 상대 주소를 쿼리하고 즉시 infowindow를 만듭니다.

희망이 있습니다.

관련 문제