简体   繁体   English

Google Maps Overlay Tiles:JSON base64 ajax调用而不是图像URL

[英]Google Maps Overlay Tiles: JSON base64 ajax call instead of image URL

I've got a Google maps custom overlay setup with Maptypes and Tiles like described here: https://developers.google.com/maps/documentation/javascript/examples/maptype-image 我有一个带有Maptypes和Tiles的Google Maps自定义叠加层设置,如下所示: https : //developers.google.com/maps/documentation/javascript/examples/maptype-image

And as long as I use a dummy image creation service everything works perfect, because the GetTileUrl part returns a direct image URL. 只要我使用虚拟图像创建服务,一切都可以完美运行,因为GetTileUrl部分返回直接图像URL。

But now here's my problem: The final application makes use of an API that needs an AJAX call that returns JSON data with Base64 encoded image. 但是,现在这是我的问题:最终应用程序利用了需要AJAX调用的API,该API返回具有Base64编码图像的JSON数据。 As soon as I use this asynchronously that API will not return data fast enough to fill a variable. 一旦我异步使用它,API将不会足够快地返回数据以填充变量。 If I use it synchronously, it all takes way to much time, freezes up the UI and more of those problems. 如果我同步使用它,这将花费大量时间,冻结UI以及更多此类问题。 So my preferred option is to use it asynchronously, but how do I get this to work. 因此,我的首选方法是异步使用它,但是如何使它起作用。

Here's the code: 这是代码:

Ajax call: Ajax呼叫:

function GetPicture(url) {
var _ajaxOptions = {
  critical: false,
  url: url,
  error: getCTerror,
  success: getGetPictureSuccess,
  cache: false
};

I THEN DO AN INIT ON AN AJAX UTIL, WHICH DOES THE SAME AS A NORMAL AJAX CALL, BUT THEN WITH PROPER SERVER LOGGING AND SUCH.

}

function getGetPictureSuccess(response) {
  // Feed the Base64 data to the URL
  the_url = response.BASE64STRING;
}

Overlay: 覆盖:

overlayOptions = {
getTileUrl: function (coord, zoom) {

  // Get the center point of the tile
  var centerpoint = new google.maps.Point((coord.x * tileSize) + (tileSize / 2), ((coord.y + 1) * 256) + (tileSize / 2));

  // Start a new Mercator projection for the x,y object
  var merc = new MercatorProjection();

  // Convert the Mercator object to Lat Long
  var centerLatLng = merc.fromDivPixelToLatLng(centerpoint, zoom);

  // Define the parameters for the Ajax call
  var apiParams = 'GetPicture?RequestorID=' + requestorID + '&Lat=' + centerLatLng.k + '&Lon=' + centerLatLng.B + '&Width=' + tileSize + '&Height=' + tileSize + '&Zoom=' + zoom + '&mapIDs=' + layers + '&includePicture=true&includePOI=true';


  ctGetPicture(apiBaseUrl + apiParams);

  if (!!the_url) {
    return "data:image/png;base64," + the_url;
  }
},
tileSize: new google.maps.Size(tileSize, tileSize),
isPng: true
};

overlayMapType = new google.maps.ImageMapType(overlayOptions);

function mapinit() {
 //Tiles overlays insert
  if (layers.length > 0) {
   map.overlayMapTypes.insertAt(0, overlayMapType);
  }
}

So how do I make this work asynchronously? 那么如何使它异步工作呢?

I ended up using a REST service that did the call for me. 我最终使用了一个REST服务来为我打电话。 So I just sent a http request to an URL which contained the REST service, have that get the image, just so that the getTileUrl function could think it was getting an URL. 因此,我只是向包含REST服务的URL发送了一个http请求,获取了该图像,以便getTileUrl函数可以认为它正在获取URL。 Without changing the API of Google (getTileUrl part) I don't think it's possible to do this any other way... 在不更改Google API(getTileUrl部分)的情况下,我认为不可能以其他方式进行此操作...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM