Replacing GTileLayer in Google Maps v3 with ImageMapType bounding Tile borders?

I need to update this code:

radar_layer.getTileUrl=function(tile,zoom) { var llp = new GPoint(tile.x*256,(tile.y+1)*256); var urp = new GPoint((tile.x+1)*256,tile.y*256); var ll = G_NORMAL_MAP.getProjection().fromPixelToLatLng(llp,zoom); var ur = G_NORMAL_MAP.getProjection().fromPixelToLatLng(urp,zoom); var dt = new Date(); var nowtime = dt.getTime(); var tileurl = "http://demo.remoteservice.com/cgi-bin/serve.cgi?"; tileurl+="bbox="+ll.lng()+","+ll.lat()+","+ur.lng()+","+ur.lat(); tileurl+="&width=256&height=256&reaspect=false&cachetime="+nowtime; return tileurl; }; 

I got:

 var DemoLayer = new google.maps.ImageMapType({ getTileUrl: function(coord, zoom) { var llp = new google.maps.Point(coord.x*256,(coord.y+1)*256); var urp = new google.maps.Point((coord.x+1)*256,coord.y*256); var ll = googleMap.getProjection().fromPointToLatLng(llp); var ur = googleMap.getProjection().fromPointToLatLng(urp); var dt = new Date(); var nowtime = dt.getTime(); var tileurl = "http://demo.remoteservice.com/cgi-bin/serve.cgi?"; tileurl+="bbox="+ll.lng()+","+ll.lat()+","+ur.lng()+","+ur.lat(); tileurl+="&width=256&height=256&reaspect=false&cachetime="+nowtime; return tileurl; }, tileSize: new google.maps.Size(256, 256), opacity:1.0, isPng: true }); 

In particular, I need help in this section:

 var llp = new google.maps.Point(coord.x*256,(coord.y+1)*256); var urp = new google.maps.Point((coord.x+1)*256,coord.y*256); var ll = googleMap.getProjection().fromPointToLatLng(llp); var ur = googleMap.getProjection().fromPointToLatLng(urp); 

The service wants a bounding box from what I understand. However, ll and ur don't seem to fix at all.

I had a job and displaying the entire bounding box in each tile, but of course, this is not what I need.

Any insight here would be greatly appreciated if it weren’t for the GTileLayers in V3, if I can get around it until I'm upset.

+4
source share
3 answers

The fix in my case was to use the updated tile service from the same publisher. It takes simpler parameters:

  globalfoovar = new google.maps.ImageMapType({ getTileUrl: function(tile, zoom) { return "http://bar.com/" + zoom + "/" + tile.x + "/" + tile.y +".png"; }, tileSize: new google.maps.Size(256, 256), opacity:0.60, isPng: true }); googleMap.overlayMapTypes.push(null); // create empty overlay entry googleMap.overlayMapTypes.setAt("0",globalfoovar); // set the overlay, 0 index // if you want to hide the layer later (toggle): // googleMap.overlayMapTypes.setAt("0",null); 

As you can see, this is much simpler than the code I posted in the problem description.

However, if you are still reading, you probably still want an answer to the original problem that converts latlng to pixels. Don’t worry, I have a solution for this.

To access the 4 useful latlng, point, and pixel transforms, you need to add a dummy OverlayView.

Step 1) Define a stub so that it is available globally along with your map and other options:

 var googleMap = {}; // global map var var mapCanvasStub = {}; // map OverlayView var 

Step 2) After rendering your map, configure this OverlayView layout / dummy as follows:

 googleMap = new google.maps.Map($('#mapCanvas')[0]); // google map init mapCanvasStub = function (map) { this.setMap(map); } // start building overlay stub mapCanvasStub.prototype = new google.maps.OverlayView(); mapCanvasStub.prototype.draw = function() {}; mapCanvasStub = new mapCanvasStub(googleMap); // bin dthe overlay to the map 

Now you have a functional OverlayView tied to a map, whenever you need to use these transformations, you can do it like this:

 var projection = mapCanvasStub.getProjection(); var position = projection.fromLatLngToContainerPixel( latLng ); // or fromContainerPixelToLatLng() // or fromDivPixelToLatLng() // or fromLatLngToDivPixel() // or fromLatLngToDivPixel() 

In my case, I use this to create custom info bubbles, here is a snippet from my click event:

 function customAlertClick(a,b,c) { var projection = mapCanvasStub.getProjection(); var position = projection.fromLatLngToContainerPixel(b); var top = (position.y + parseInt($('#mapCanvas').position().top)); var lft = (position.x + parseInt($('#mapCanvas').position().left)); // obviously this is an overly simple example $('#foo').css('top',top + 'px'); $('#foo').css('lft',left + 'px'); } 

Hope this helps someone else to jump into V3.

If you're wondering why we should jump over these extra hoops to make something so simple in V2, the answer is pretty obvious when you think about it. V3 is configured to work on mobile devices, where bandwidth and processor power are limited. They eliminate as much code as possible in order to get simple map rendering, and they expect you to manually enable these additional services if you absolutely need them.

If I had more time, I would write sample.html, but that should make you.

+11
source

Thanks for the details. In fact, in my case, I could not change the API to use the tile numbers that I need to pass to LatLng. I found this solution ... so far this works for me:

 var myMapOptions = { getTileUrl: function(coord,zoom) { var proj = map.getProjection(); var zfactor=Math.pow(2,zoom); var top=proj.fromPointToLatLng(new google.maps.Point(coord.x*256/zfactor,coord.y*256/zfactor)); var bot=proj.fromPointToLatLng(new google.maps.Point((coord.x+1)*256/zfactor,(coord.y+1)*256/zfactor)); url = "/layer/layer_"+zoom+"_"+top.lng().toFixed(6)+"_"+bot.lat().toFixed(6)+"_"+bot.lng().toFixed(6)+"_"+top.lat().toFixed(6)+".png"; return url; }, tileSize: new google.maps.Size(256, 256), isPng: true, opacity: 0.4 } 
+4
source

It worked for me

I used MapLock.Light Style Tileset at the top of Google API V3

 function loadMap() { // Default the map view to the continental US mapOptions = { center: new google.maps.LatLng(17.387140, 78.491684), mapTypeId: google.maps.MapTypeId.ROADMAP, zoom: 8 }; map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); var imageMapType = new google.maps.ImageMapType({ getTileUrl: function(coord, zoom) { return "https://api.mapbox.com/v4/mapbox.light/" +zoom+"/"+coord.x+"/"+coord.y+"@2x.png?access_token=<<access_token>>"; }, tileSize: new google.maps.Size(256, 256) }); map.overlayMapTypes.push(imageMapType); } loadMap(); 
0
source

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


All Articles