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

时间:2022-01-09 16:22:48

I have the following code for requesting map data from OSM:

我有以下代码从OSM请求地图数据:

$.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" );
});

When I test the request on Overpass Turbo, it runs without any issues, but when preforming this request in a JavaScript, I always get the error:

当我在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>"

There must be something wrong with the way I do the request, but I cannot figure out what could be wrong with it.

我执行请求的方式肯定有问题,但我无法弄清楚它可能出现什么问题。

How do I get the positions of all schools in Berlin from a JavaScript?

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

I also tried using $.getJSON() but that didn't work for me either.

我也试过使用$ .getJSON(),但这对我也没有用。

1 个解决方案

#1


3  

The URL you use in your example seems to be incomplete: it should read ...interpreter?data=[out:json].... i.e. the data= part was missing.

您在示例中使用的URL似乎不完整:它应该读取... interpreter?data = [out:json] ....即data = part缺失。

As a reference you can also put your query in overpass turbo and simply click on Export -> raw data directly from Overpass API to get to a working URL. Maybe try this first with wget and if it works out ok, put the URL in your Javascript code.

作为参考,您还可以将您的查询放在立交桥turbo中,只需直接从Overpass API单击Export - > raw data即可转到工作URL。也许首先使用wget尝试这个,如果确定没问题,请将URL放在您的Javascript代码中。

Maybe you also want to study a bit, how overpass turbo does the (POST-based) calls to Overpass API: please see https://github.com/tyrasd/overpass-turbo/blob/master/js/overpass.js#L581 for details.

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

#1


3  

The URL you use in your example seems to be incomplete: it should read ...interpreter?data=[out:json].... i.e. the data= part was missing.

您在示例中使用的URL似乎不完整:它应该读取... interpreter?data = [out:json] ....即data = part缺失。

As a reference you can also put your query in overpass turbo and simply click on Export -> raw data directly from Overpass API to get to a working URL. Maybe try this first with wget and if it works out ok, put the URL in your Javascript code.

作为参考,您还可以将您的查询放在立交桥turbo中,只需直接从Overpass API单击Export - > raw data即可转到工作URL。也许首先使用wget尝试这个,如果确定没问题,请将URL放在您的Javascript代码中。

Maybe you also want to study a bit, how overpass turbo does the (POST-based) calls to Overpass API: please see https://github.com/tyrasd/overpass-turbo/blob/master/js/overpass.js#L581 for details.

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