2014-10-31 121 views
1

我有一個地圖,它有自己的數據集 - 所以它從外部API加載附加信息,填寫自定義谷歌地圖。Google Map API - 在當前地圖上的自動完成搜索

我使用的是自動從谷歌選擇功能,所以當對某個地址或位置的用戶搜索,他們放大當前地圖上(而不會丟失所有數據點)

因此,與此功能,輸出是正確的:

London 51.5073509 -0.12775829999998223 6 

我的問題 - 查詢是,我怎麼能得到如下:

console.log(defaultArea + ' ' + defaultLat + ' ' + defaultLng + ' ' + defaultZoom); 

進入當前地圖。

我試過 - 仍然沒有工作。

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

當前JQUERY:

var defaultLat = 54 
defaultLng = -4, 
defaultZoom = 6, 
defaultArea = null; 

function searchFunct() { 
    var input = document.getElementById('location'); 
    var options = { 
     types: ['(regions)'], 
     componentRestrictions: { 
      country: 'uk' 
     } 
    }; 

    var autocomplete = new google.maps.places.Autocomplete(input, options); 

    google.maps.event.addListener(autocomplete, 'place_changed', function() { 
     var place = autocomplete.getPlace(); 
     document.getElementById('city2').value = place.name; 
     document.getElementById('cityLat').value = place.geometry.location.lat(); 
     document.getElementById('cityLng').value = place.geometry.location.lng(); 

     var citi = document.getElementById('city2').value; 
     var lati = document.getElementById('cityLat').value; 
     var longi = document.getElementById('cityLng').value; 

     defaultArea = citi; 
     defaultLat = lati; 
     defaultLng = longi; 
     defaultZoom = 6; 

     console.log(defaultArea + ' ' + defaultLat + ' ' + defaultLng + ' ' + defaultZoom); 

    }); 
} 
+0

你的地圖代碼在哪裏? – geocodezip 2014-10-31 22:57:41

回答

1

添加console.log

我所做的一切有下以下聲明定義你latitudelongitude變量中的谷歌地圖中的變量google.maps.LatLng。其次,聲明一個myOptions對象來定義輸入的地方。最後,在map_canvas上繪製所有這些圖標並在其上添加一個標記。

var latlng = new google.maps.LatLng(defaultLat, defaultLng); 
var myOptions = { 
    zoom: 11, 
    center: latlng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
}; 

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

marker = new google.maps.Marker({ 
    position: latlng, 
    map: map, 
    title: 'Main map' 
});