2014-03-06 3 views
0

우편 번호를 기반으로 한 마커가있는 Google지도를 표시하려고합니다. 위도lng 좌표로 우편 번호를 변환했지만지도가 전혀 표시되지 않는 것이 문제입니다. 여기우편 번호가있는 마커가있는 Google지도를 표시 할 수 없습니다.

내가 그렇게하려고하고 방법은 다음과 같습니다

function initialize() { 
    var lat = ''; 
    var lng = ''; 
    var geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({ 'address': 94301}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     lat = results[0].geometry.location.lat(); 
     lng = results[0].geometry.location.lng(); 
     var mapOptions = { 
      zoom: 8, 
      center: new google.maps.LatLng(lat, lng) 
     }; 
     var map = new google.maps.Map(document.getElementById("gmap"), myOptions); 
     map.setCenter(results[0].geometry.location); 
     var marker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location 
     }); 
     alert('111'); 
     } else { 
     alert("Geocode was not successful for the following reason: " + status); 
     } 
    }); 
    google.maps.event.addDomListener(window, 'load', initialize); 
} 

심지어 경고 ('111')가 등장하지 않습니다.

내가 뭘 잘못하고 있니?

감사

+1

당신이 자바 스크립트 콘솔을 확인 했습니까? 해당 경고가 표시되지 않는 경우 해당 행에 도달하기 전에 문제가 있다고 의심됩니다. –

+0

콘솔에 오류가없고 경고가 없습니다. 마커에 문제가있는 것 같아요, 단순한지도가 잘 작동하고 버그를 찾을 수 없습니다. – user984621

+0

마커 생성 및 console.log를 제거하면 위도가 작동합니다. –

답변

0

당신이 초기화 기능 초기화 함수 내 전화하는거야, 귀하의 문제는 여기!

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

마지막 줄을 그 기능으로 이동하면 내가 생각하는 결과가 표시됩니다.

function initialize() { 
    ... 
} 
google.maps.event.addDomListener(window, 'load', initialize); 
0

2 문제 :

  1. onload 이벤트에 대한 리스너가 초기화 함수 내에서, 그래서 내가 자바 스크립트 오류가 있음을 해결할 때 결코 초기화
  2. 를 호출하지

    Uncaught InvalidValueError: in property address: not a string 
    
  3. 다음 :

    Uncaught ReferenceError: myOptions is not defined 
    

그래서 : 오류가 없는지

 function initialize() { 
     var lat = ''; 
     var lng = ''; 
     var geocoder = new google.maps.Geocoder(); 
     geocoder.geocode({ 'address': "94301"}, function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
      lat = results[0].geometry.location.lat(); 
      lng = results[0].geometry.location.lng(); 
      var mapOptions = { 
       zoom: 8, 
       center: new google.maps.LatLng(lat, lng) 
      }; 
      var map = new google.maps.Map(document.getElementById("gmap"), mapOptions); 
      map.setCenter(results[0].geometry.location); 
      var marker = new google.maps.Marker({ 
       map: map, 
       position: results[0].geometry.location 
      }); 
      alert('111'); 
      } else { 
      alert("Geocode was not successful for the following reason: " + status); 
      } 
     }); 

    } 
     google.maps.event.addDomListener(window, 'load', initialize); 
관련 문제