2016-07-13 6 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、 中心:緯度と経度、 mapTypeId:google.maps.MapTypeId.ROADMAP }。 var map =新しいgoogle.maps.Map(document.getElementById( "map-canvas")、 myOptions); } google.maps.event.addDomListener(window、 "load"、initialize);これは私のコードだがmap.setCenter(結果[0] .geometry.location);この行のエラーは –

+0

のみです。コードをコメントに貼り付けるのではなく、質問を更新する必要があります。本当に読みにくいです。 – SeanKendle

+0

また、jsfiddleを作成することを検討してください。 – SeanKendle

関連する問題