2017-05-25 56 views
-3

后,我想找到之前把加载图标或我需要赶上找到位置开始/结束函数(如AJAX前/成功函数)谷歌地图API前/功能(如AJAX)

我找不到任何与此相关的资源。我想要做的;当你点击“查找当前位置”按钮时,你会看到一个小图标。隐藏图标当进程正在使用Geocoding service代码完成

我使用JavaScript没有jQuery的

回答

1

,我加了<div id="loader"></div><div id="floating-panel"></div>。在这我已经添加图像标记时geocodeAddress(geocoder, resultsMap)被调用。您可以使用所需的图标

function initMap() { 
 
    var map = new google.maps.Map(document.getElementById('map'), { 
 
    zoom: 8, 
 
    center: { 
 
     lat: -34.397, 
 
     lng: 150.644 
 
    } 
 
    }); 
 
    var geocoder = new google.maps.Geocoder(); 
 

 
    document.getElementById('submit').addEventListener('click', function() { 
 
    geocodeAddress(geocoder, map); 
 
    }); 
 

 
} 
 

 
function geocodeAddress(geocoder, resultsMap) { 
 
    document.getElementById('loader').innerHTML = "<img src='https://media.giphy.com/media/3oEjI6SIIHBdRxXI40/giphy.gif'>" 
 
    var address = document.getElementById('address').value; 
 
    geocoder.geocode({ 
 
    'address': address 
 
    }, function(results, status) { 
 
    if (status === 'OK') { 
 
     document.getElementById('loader').innerHTML = "" 
 
     resultsMap.setCenter(results[0].geometry.location); 
 
     var marker = new google.maps.Marker({ 
 
     map: resultsMap, 
 
     position: results[0].geometry.location 
 
     }); 
 
    } else { 
 
     document.getElementById('loader').innerHTML = "" 
 
     alert('Geocode was not successful for the following reason: ' + status); 
 
    } 
 
    }); 
 
    /*google.maps.event.addListenerOnce(map, 'idle', function(){ 
 
    alert() 
 
});*/ 
 

 
}
/* Always set the map height explicitly to define the size of the div 
 
* element that contains the map. */ 
 

 
#map { 
 
    height: 100%; 
 
} 
 

 

 
/* Optional: Makes the sample page fill the window. */ 
 

 
html, 
 
body { 
 
    height: 100%; 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 

 
#floating-panel { 
 
    position: absolute; 
 
    top: 10px; 
 
    left: 25%; 
 
    z-index: 5; 
 
    background-color: #fff; 
 
    padding: 5px; 
 
    border: 1px solid #999; 
 
    text-align: center; 
 
    font-family: 'Roboto', 'sans-serif'; 
 
    line-height: 30px; 
 
    padding-left: 10px; 
 
}
<div id="floating-panel"> 
 
    <input id="address" type="textbox" value="Sydney, NSW"> 
 
    <input id="submit" type="button" value="Geocode"> 
 
    <div id="loader"> 
 
    </div> 
 
</div> 
 
<div id="map"></div> 
 
<!-- Replace the value of the key parameter with your own API key. --> 
 
<script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap"> 
 
</script>

+0

耶士改变!完全如我所愿 – vulkan