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

时间:2015-07-18 14:27:51

标签: javascript jquery ajax openstreetmap overpass-api

我有以下用于从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" );
});

当我在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(),但这对我来说也不起作用。

1 个答案:

答案 0 :(得分:3)

您在示例中使用的网址似乎不完整:它应该显示为... MAKEFILE_GENERATOR = UNIX TARGET_PLATFORM = unix TEMPLATE = app CONFIG += qt warn_on release incremental link_prl gdb_dwarf_index QT += core gui QMAKE_INCREMENTAL_STYLE = sublib include(../common/linux.conf) include(../common/gcc-base-unix.conf) include(../common/g++-unix.conf) QMAKE_CC = arm-linux-gnueabihf-gcc-4.8 QMAKE_CXX = arm-linux-gnueabihf-g++-4.8 QMAKE_LINK = arm-linux-gnueabihf-g++-4.8 QMAKE_LINK_SHLIB = arm-linux-gnueabihf-g++-4.8 QMAKE_AR = arm-linux-gnueabi-ar cqs QMAKE_OBJCOPY = arm-linux-gnueabi-objcopy QMAKE_STRIP = arm-linux-gnueabi-strip load(qt_config) ....即 data = 部分缺失。

作为参考,您还可以将您的查询放在立交桥turbo中,只需点击导出 - &gt;原始数据直接来自Overpass API以获取工作URL。也许首先使用wget尝试这个,如果确定没问题,请将URL放在Javascript代码中。

也许您还想学习一下,如何通过立交桥turbo(基于POST)调用Overpass API:有关详细信息,请参阅https://github.com/tyrasd/overpass-turbo/blob/master/js/overpass.js#L581