谷歌地图自动完成时单击按钮获取区域

时间:2014-08-19 06:44:13

标签: javascript google-maps-api-3

我尝试了很多以获得与谷歌地图搜索栏相同的结果。 我在谷歌地图的搜索栏中使用以下代码。

google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
    //alert(place.address_components.length);
     for (var i=0; i<place.address_components.length; i++) {
         for (var b=0;b<place.address_components[i].types.length;b++) {
            if (place.address_components[i].types[b] == "route") {
                    var route1= place.address_components[i];
                     route1 = route1.long_name;
                     undefined = true;
             }else if (place.address_components[i].types[b] == "sublocality") {
                  var   city1= place.address_components[i];
                     city1 = city1.long_name;
                     undefined = true;
             }else if (place.address_components[i].types[b] == "locality") {
                    var maincity= place.address_components[i];
                     maincity = maincity.long_name;
                     undefined = true;
             }else if (place.address_components[i].types[b] == "administrative_area_level_1") {
                    var state1= place.address_components[i];
                     state1 = state1.long_name;
                     undefined = true;
             }
         }
     }
});

现在上面工作正常。 但是当我搜索该区域并单击按钮以获得与自动完成返回相同的结果时,我已多次尝试声明全局变量地点 但仍然没有得到结果。

当用户点击搜索按钮时,我尝试使用以下代码。

 <div id="type-selector" class="controls"  onClick="search_click('sclick');" style="z-index: 2;position: absolute;top: -3px;">
    <label for="changetype-all">Search</label>    
 </div>

以及点击搜索按钮时的以下功能

var get_add = document.getElementById("autocomplete").value;
var address = get_add;
geocoder = new google.maps.Geocoder();
            geocoder.geocode({
            'address': address
            }, function(results, status) {      
                var lat=results[0].geometry.location.lat();    
                var lng=results[0].geometry.location.lng(); 
var stockholm = new google.maps.LatLng(lat, lng);
 geocoder.geocode({'latLng': stockholm}, function(results, status) {
     if (status == google.maps.GeocoderStatus.OK) {
         //console.log(results);
         //alert(results[1]);
         //alert(results[0]);
         if (results[1]) {
             for (var i=0; i<results[1].address_components.length; i++) {
                 for (var b=0;b<results[1].address_components[i].types.length;b++) {
                    if (results[1].address_components[i].types[b] == "street_address") {
                             route1= results[1].address_components[i];
                             route1 = route1.long_name;
                     }
                    if (results[1].address_components[i].types[b] == "sublocality") {
                         city1= results[1].address_components[i];
                         city1 = city1.long_name;
                     }
                    if (results[1].address_components[i].types[b] == "administrative_area_level_1") {
                         state1= results[1].address_components[i];
                         state1 = state1.long_name;
                     }
                    if (results[1].address_components[i].types[b] == "country") {
                             country= results[1].address_components[i];
                             country = country.long_name;
                    }
                 }
             }
         }else if(results[0])
         {
             for (var i=0; i<results[0].address_components.length; i++) {
                 for (var b=0;b<results[0].address_components[i].types.length;b++) {
                    if (results[0].address_components[i].types[b] == "street_address") {
                             route1= results[0].address_components[i];
                             route1 = route1.long_name;
                     }
                    if (results[0].address_components[i].types[b] == "sublocality") {
                         city1= results[0].address_components[i];
                         city1 = city1.long_name;
                     }
                    if (results[0].address_components[i].types[b] == "administrative_area_level_1") {
                         state1= results[0].address_components[i];
                         state1 = state1.long_name;
                     }
                    if (results[0].address_components[i].types[b] == "country") {
                             country= results[0].address_components[i];
                             country = country.long_name;
                     }
                 }
             }
         }

但这并没有给出与自动完成返回相同的结果。

请帮助我有没有其他方法可以做或我必须做什么才能同时获得相同的结果。

0 个答案:

没有答案