2016-07-13 12 views
-1
// this function search location and show on map 

function showAddressTextBox(address) { // showAddressTextBox function search location and show on map 
    // alert(address); 
    var infowindow2 = new google.maps.InfoWindow(); //this infowindows 
    var geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({ 
     'address': address 
    }, function (results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      map.setCenter(results[0].geometry.location); 
      var marker = new google.maps.Marker({ 
       map: map, 
       position: results[0].geometry.location 
      }); 
      google.maps.event.addListener(marker, 'click', function() { 
       infowindow2.setContent('<div><strong>Address:' + results[0].formatted_address + '</strong><br>' + 
        'Place ID: ' + results[0].place_id + '</div>'); 
       infowindow2.open(map, marker); 
      }); 
      //   path = address; 
      //   lat = results[0].geometry.location.lat(); 
      //   lon = results[0].geometry.location.lng(); 
     } else { 
      //alert("Geocode was not successful for the following reason: " + status); 
     } 
    }); 
} //end function 

답변

0

매우 간단합니다. map 변수는 정의되지 않았습니다!

전체 코드가 보이지 않습니까? map 변수는 어디에 정의되어 있습니까?

이 예제에서는 map 변수를 정의하고 실제 맵을 초기화해야한다는 것을 보여줍니다.

var map; 
function initialize() { 
    geocoder = new google.maps.Geocoder(); 
    var latlng = new google.maps.LatLng(-34.397, 150.644); 
    var mapOptions = { 
     zoom: 8, 
     center: latlng 
    } 
    map = new google.maps.Map(document.getElementById("map"), mapOptions); 
    } 

여기에 대한 자세한 내용보기 : https://developers.google.com/maps/documentation/javascript/geocoding

+0

VAR지도; function initialize() { var latlng = new google.maps.LatLng (21.15, 79.04); var myOptions = { 줌 : 8, 센터 : latlng, mapTypeId : google.maps.MapTypeId.ROADMAP}}; var map = new google.maps.Map (document.getElementById ("map-canvas"), myOptions); } google.maps.event.addDomListener (window, "load", initialize); 이건 내 코드지만 map.setCenter (results [0] .geometry.location); 이 줄은 오직 –

+0

오류 수정 코드를 주석에 붙여 넣기보다는 업데이트해야합니다. 정말 읽기가 어렵습니다. – SeanKendle

+0

또한 jsfiddle를 만드는 것을 고려하십시오. – SeanKendle

관련 문제