2017-03-15 60 views
-2

我使用JavaScript进行编程,并且我有API,它给了我一个JSON对象。 这是 - >http://193.70.60.44:3000/geoserver/taxi_server/api/v1.0/taxi对象网址。从JavaScript中的外部url中获取JSON对象

在这个JSON对象中有JSONOBJECT.position.coordinates [0],它给了我一个位置的经度。

我如何获得JavaScript中的JSON对象并使用坐标?

+1

你的API提供了响应:'{“消息”:“欢迎来到虚无的开始。”} ' –

+1

发布的URL没有Access-Control-Allow-Origin标头,它不是JSONP,因此无法直接在浏览器中获取该JSON – adeneo

回答

0

像这样使用jQuery(编辑),但在你返回的对象使用适当的属性:

var myUrl = 'http://siteofjson.org/getjson/'; 
$.getJSON(myUrl, function(JSONOBJECT){ 
    console.log(JSONOBJECT.position.coordinates[0]); 
    var lonLat = new OpenLayers.LonLat(JSONOBJECT.position.coordinates[0].lon,JSONOBJECT.position.coordinates[0].lat) 
     .transform(
      new OpenLayers.Projection("EPSG:4326"), // Transformation aus dem Koordinatensystem WGS 1984 
      map.getProjectionObject() // in das Koordinatensystem 'Spherical Mercator Projection' 
     ); 
});