【问题标题】:Phonegap - Can't access GeoJson Stored in Data DirectoryPhonegap - 无法访问存储在数据目录中的 GeoJson
【发布时间】:2016-11-03 17:48:30
【问题描述】:

我正在尝试使用 OpenLayers 3 将可更新的矢量图层合并到我的 phonegap 构建中。它将检查服务器的更新并在必要时下载新文件。

我可以下载文件并将其存储没有问题(我在其他应用程序中对非 geojson 文件类型使用相同的代码没有问题)。我已经使用以下方法验证了它的存在:

var resortURL = cordova.file.dataDirectory + "resorts.geojson";
window.resolveLocalFileSystemURL(resortURL,function(e){
   console.log("located");
},function(e){
   console.log("missing");
});

当我使用 OL 访问文件时:

var resortSource = new ol.source.Vector({
    url: resortURL,
    format: new ol.format.GeoJSON()
});
var resortBoundary = new ol.layer.Vector({
    source: resortSource
});
map.addLayer(resortBoundary);

上述方法未能将任何数据添加到地图,尽管它确实添加了图层。我更进一步,将 geojson 文件直接添加到资产 www 文件夹 pre-build 并使用:

url: "json/resorts.geojson"

从资产位置请求文件完全符合预期。问题是当我将它下载到 Android 上的数据目录后尝试访问它,然后尝试通过那里访问。我错过了什么吗?我一直在尝试一切,试图让它发挥作用。

【问题讨论】:

  • 您的 Web 控制台对“网络”选项卡下的 geojson 文件有何看法?它是否显示对geojson文件的请求?读了吗?? resortSource.getFeatures().length 显示什么??
  • @GoinOff 数组长度为零。网络选项卡显示请求以 0 B 的文件大小发出。除了网络选项卡中的状态显示“已完成”而不是典型的 200 之外,它看起来与它工作时相同。我以前从未见过。
  • 我在使用ol.source.Vectorurl 属性时遇到了许多浏览器缓存问题,它使用了旧版本的geojson 文件。您是否尝试过清除网络浏览器中的缓存?你确定geojson文件包含有效的geojson吗? geojson.org/geojson-spec.html
  • @GoinOff 我可以,但我认为这不是问题所在。此外,我在浏览器缓存中存储了我不想删除的信息!

标签: cordova phonegap-build openlayers-3 geojson


【解决方案1】:

考虑使用矢量加载器函数来处理获取您的 geojson 文件。我有很多缓存问题,没有使用矢量加载函数来获取 geojson 文件,下面的方法消除了这个问题。例如:

var utils = {
    refreshGeoJson: function(source,url) {
      var now = Date.now();
      if (typeof url == 'undefined') {
        url = source.getUrl();
      }
      url += '?t=' + now; //Add current time to prevent browser caching
      console.info('refreshGeoJson url: ' + url);
          this.getJson(url).when({
        ready: function(response) {
          var format = new ol.format.GeoJSON();
          var features = format.readFeatures(response, {
            featureProjection: 'EPSG:3857'
          });
          console.dir(features);
          console.log(features.length);
          source.addFeatures(features);
          source.changed();
        }
         });
},
    getJson: function(url) {
          var xhr = new XMLHttpRequest(),
          when = {},
          onload = function() {
        console.log('xhr.status onload() ' + xhr.status);
        if (xhr.status === 200) {
         console.log('getJson() xhr: ');
         console.dir(xhr);
         console.log('getJson() xhr.response: ');
         console.dir(xhr.response);
                  when.ready.call(undefined, JSON.parse(xhr.response));
            }
        if (xhr.status === 404) {
          console.log('file not found! url: ' + url);
          alert("file not found!\n" + url);
        }
          },
          onerror = function() {
            console.info('Cannot XHR ' + JSON.stringify(url));
          };
      //console.log('getJson() - retrieve file url: ' + url);
          xhr.open('GET', url, true);
          xhr.setRequestHeader('cache-control', 'no-store');
          xhr.onload = onload;
          xhr.onerror = onerror;
          xhr.send(null);
          return {
        when: function(obj) { when.ready = obj.ready; }
        };
    }
};

var vectorLoader = function(extent, resolution, projection) {
  utils.refreshGeoJson(this);
}

var resortSource = new ol.source.Vector({
    url: /path-to/myFile.geojson',
    format: new ol.format.GeoJSON({
       defaultDataProjection :'EPSG:3857' 
    }),
    loader: vectorLoader,
    strategy: ol.loadingstrategy.all
  });

如果您的 geojson 文件存在语法问题,它将在此处失败when.ready.call(undefined, JSON.parse(xhr.response));。这使您可以更好地控制事物,而不仅仅是使用没有矢量加载器功能的 url 属性。

【讨论】:

  • 所以我试了一下,但仍然没有骰子,我很欣赏带有评论的完整代码!它记录了“xhr.status onload() 0”。
  • 由于您从 XHR 调用中收到 0(零)返回码,我会查看以下帖子:stackoverflow.com/questions/3825581/…stackoverflow.com/questions/872206/…
  • 另外,检查你的网络服务器日志文件,看看请求是否发送到服务器???
猜你喜欢
  • 1970-01-01
  • 2019-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-15
  • 2020-04-30
  • 2018-04-29
  • 1970-01-01
相关资源
最近更新 更多