javascript - 如何使用 jQuery 的 Overpass API 获取 OSM 数据?

标签 javascript jquery ajax openstreetmap overpass-api

我有以下用于从 OSM 请求 map 数据的代码:

$.ajax({
    url:
        'https://www.overpass-api.de/api/interpreter?' + 
        '[out:json][timeout:60];' + 
        'area["boundary"~"administrative"]["name"~"Berlin"];' + 
        'node(area)["amenity"~"school"];' + 
        'out;',
    dataType: 'json',
    type: 'GET',
    async: true,
    crossDomain: true
}).done(function() {
    console.log( "second success" );
}).fail(function(error) {
    console.log(error);
    console.log( "error" );
}).always(function() {
    console.log( "complete" );
});

当我在 Overpass Turbo 上测试请求时,它运行没有任何问题,但在 JavaScript 中执行此请求时,我总是会收到错误:

"<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>  <meta http-equiv="content-type" content="text/html; charset=utf-8" lang="en"/>  <title>OSM3S Response</title></head><body><p>The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.</p><p><strong style="color:#FF0000">Error</strong>: line 1: parse error: Key expected - '%' found. </p><p><strong style="color:#FF0000">Error</strong>: line 1: parse error: '!', '~', '=', '!=', or ']'  expected - '%' found. </p><p><strong style="color:#FF0000">Error</strong>: line 1: parse error: Value expected - '%' found. </p><p><strong style="color:#FF0000">Error</strong>: line 1: parse error: ',' or ']' expected - '%' found. </p><p><strong style="color:#FF0000">Error</strong>: line 1: parse error: Key expected - '%' found. </p><p><strong style="color:#FF0000">Error</strong>: line 1: parse error: '!', '~', '=', '!=', or ']'  expected - '%' found. </p><p><strong style="color:#FF0000">Error</strong>: line 1: parse error: Value expected - '%' found. </p><p><strong style="color:#FF0000">Error</strong>: line 1: parse error: ',' or ']' expected - '%' found. </p><p><strong style="color:#FF0000">Error</strong>: line 1: static error: For the attribute "k" of the element "has-kv" the only allowed values are non-empty strings. </p><p><strong style="color:#FF0000">Error</strong>: line 1: static error: For the attribute "k" of the element "has-kv" the only allowed values are non-empty strings. </p><p><strong style="color:#FF0000">Error</strong>: line 1: parse error: Key expected - '%' found. </p><p><strong style="color:#FF0000">Error</strong>: line 1: parse error: '!', '~', '=', '!=', or ']'  expected - '%' found. </p><p><strong style="color:#FF0000">Error</strong>: line 1: parse error: Value expected - '%' found. </p><p><strong style="color:#FF0000">Error</strong>: line 1: parse error: ',' or ']' expected - '%' found. </p><p><strong style="color:#FF0000">Error</strong>: line 1: static error: For the attribute "k" of the element "has-kv" the only allowed values are non-empty strings. </p></body></html>"

肯定是我请求的方式有问题,但我想不通到底哪里出了问题。

如何通过 JavaScript 获取柏林所有学校的位置?

我也尝试过使用 $.getJSON() 但这对我也不起作用。

最佳答案

您在示例中使用的 URL 似乎不完整:它应该显示为 ...interpreter?data=[out:json].... 即 data= 部分丢失。

作为引用,您还可以将查询放入 overpass turbo 中,然后只需单击“导出”->“直接来自 Overpass API 的原始数据”即可获得有效的 URL。也许先用 wget 试试这个,如果没问题,把 URL 放到你的 Javascript 代码中。

也许您还想研究一下,overpass turbo 如何(基于 POST)调用 Overpass API:请参阅 https://github.com/tyrasd/overpass-turbo/blob/master/js/overpass.js#L581了解详情。

关于javascript - 如何使用 jQuery 的 Overpass API 获取 OSM 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31492163/

相关文章:

javascript - 无法使用 jquery 在第二张图片后添加内容

javascript - 尝试创建交易时,在 hyperledger Composer 中出现错误 : Expected a Resource or Concept.

javascript - 为什么我无法从 ajax post 请求中获取操作

php - 2 列的 CSS 布局问题

javascript - 如何将 onchange 选定的下拉值存储到隐藏的输入字段中?

javascript - JS中的多行输出

javascript - Angular 如何在 IE9/10 中渲染自定义 html 标签而不让浏览器感到不安?

javascript - 无法让滚动动画与 jquery 一起使用

jquery - jquery 中裁剪\缩放工具的几何计算

javascript - 在 getscript 内部定义变量并在其外部使用它? (从 json 文件中获取头像并循环使用)