簡體   English   中英

Google Maps Overlay Tiles:JSON base64 ajax調用而不是圖像URL

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

我有一個帶有Maptypes和Tiles的Google Maps自定義疊加層設置,如下所示: https : //developers.google.com/maps/documentation/javascript/examples/maptype-image

只要我使用虛擬圖像創建服務,一切都可以完美運行,因為GetTileUrl部分返回直接圖像URL。

但是,現在這是我的問題:最終應用程序利用了需要AJAX調用的API,該API返回具有Base64編碼圖像的JSON數據。 一旦我異步使用它,API將不會足夠快地返回數據以填充變量。 如果我同步使用它,這將花費大量時間,凍結UI以及更多此類問題。 因此,我的首選方法是異步使用它,但是如何使它起作用。

這是代碼:

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;
}

覆蓋:

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);
  }
}

那么如何使它異步工作呢?

我最終使用了一個REST服務來為我打電話。 因此,我只是向包含REST服務的URL發送了一個http請求,獲取了該圖像,以便getTileUrl函數可以認為它正在獲取URL。 在不更改Google API(getTileUrl部分)的情況下,我認為不可能以其他方式進行此操作...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM