How to determine the progress downloaded for GroundOverlay

I am uploading 10 GroundOverlays to GoogleMap and would like to show the progress related to uploading images.

var imageBounds = new google.maps.LatLngBounds( new google.maps.LatLng(42.80059,-115.253806), new google.maps.LatLng(47.481725,-110.227471)); var layer1 = new google.maps.GroundOverlay("overlays/layer1.png",imageBounds); layer1.setOpacity(.5); layer1.setMap(map); 

How to determine when the actual image of each overlay is loaded or% is loaded? I would like to add a progress bar or something to show the user that overlays are loading, as this can take about 5 seconds.

+4
source share
2 answers

If you have one image as a groundoverlay method, I would do the following:

  • Create img tag via javascript
  • Set src to overlays / layer1.png
  • display waiting window
  • Add the onload event to it, on which load it as a gmaps overlay and close the wait window.

Progress ... it's a bit more for the browser. But downloading is easy, therefore:

 function loadOverlay(){ var img = new Image(); var img_url = "overlays/layer1.png" img.src= img_url; $('#wait_for_overlay').show(); img.onLoad = function(){ $('#wait_for_overlay').hide(); var layer1 = new google.maps.GroundOverlay(img_url,imageBounds); layer1.setOpacity(.5); layer1.setMap(map); } }; 

The reason this will work:

Creating an img object and setting its 'src' attribute will cause the browser to start loading the requested file as soon as the javascript function does this.

The browser will put this image in its local cache, and then call the onLoad function to see what should happen to it.

The callback actually does nothing for the img variable (perhaps it should make sure that it will not be destroyed in accordance with the closing rules, make a NOP with it if this is an error), but instead it calls the google-maps function.

The google-maps function will request an image at the same URL in which the browser will look for its cache table and immediately return the image from the cache.

This trick is nasty, but in fact, the Google Maps engine will do the same thing onLoad thingy in the background (since this works with the map).

Hope this satisfies your question ... there is no progress bar, just a progress bar ... Perhaps you could make a progress bar by querying it with XHR and check the progress, I'm not sure, but the trick will be the same: make a faux request, so that he gets into the cache.

+4
source

I doubt you will. You can request new features added to the GroundOverlay class so that users have more access and control the uploaded image. I would like to have such features for my projects.

0
source

Source: https://habr.com/ru/post/1415353/


All Articles