2012-04-28 16 views
0

我正在嘗試從網頁中讀取JSON文件,並在OpenLayers中的地圖上顯示文件中包含的路徑。我發現了另一個與我的類似的例子,How to fetch JSON from a URL using JavaScript?,但我無法得到它的工作。從JavaScript中調用JSON文件

我創建包含的URL字符串,例如,像這樣:

http://router.project-osrm.org/viaroute?rebuild=1&loc=43.46711564169348,-3.880102031707764&loc=43.4669443349282,-3.862788677215576&output=json 

本網頁應該返回一個JSON文件,我必須跟着我達到終點的點。我知道這是有效的,因爲我試過這個例子:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
    <title>Open Space Web-Map builder Code</title> 
</head> 
<body> 
    <div class="header-content"> 
    [ 
    <a class="result-link" onClick="document.location.href='http://router.project- osrm.org/viaroute? rebuild=1&amp;loc=43.46711564169348,-3.880102031707764&amp;loc=43.4669443349282,-3.862788677215576&amp;output=json';">Generar ruta</a> 
    ] 
    </div> 
</body> 
</html>​ 

然後它返回一個JSON文件,如下所示。但是如果我嘗試使用我的頁面,它不起作用。我有這個函數讀取JSON文件:

function pintarRutaCamion() { 
    capaRuta = new OpenLayers.Layer.Vector("capaRuta"); 
    var style_green = { 
     strokeColor: "#00FF00", 
     strokeOpacity: 1, 
     strokeWidth: 6 
    }; 
    var pointRuta = []; 

    alert(rutaCompleta); //show the complete url 
    JQ.getJSON(rutaCompleta, function(puntosRuta) { 
     alert(puntosRuta.route_geometry.length); //show size of returned json file 
     for (i = 0; i < puntosRuta.route_geometry.length; i++) { 
      coordenadas = new OpenLayers.LonLat(puntosRuta.route_geometry[i][1], puntosRuta.route_geometry[i][0]).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject()); 
      pointruta.push(coordenadas); 
     } 

    }); 

    //create a polyline feature from the array of points 
    var lineString = new OpenLayers.Geometry.LineString(pointRuta); 
    var lineFeature = new OpenLayers.Feature.Vector(lineString, null, style_green); 
    capaRuta.addFeatures([lineFeature]); 
    //add it to the map 
    map.addLayer(capaRuta); 
}​ 

的JSON文件應該是這樣的:

{"version": 0.3, 
"status": 0, 
"status_message": "Found route between points", 
"route_geometry": [[43.46716, -3.87987],[43.46668, -3.87963],[43.46706, -3.87761],[43.46593, -3.87740],[43.46571, -3.87552],[43.46559, -3.87515],[43.46553, -3.87512],[43.46549, -3.87504],[43.46548, -3.87496],[43.46550, -3.87487],[43.46554, -3.87482],[43.46558, -3.87433],[43.46533, -3.87210],[43.46535, -3.87185],[43.46546, -3.87128],[43.46592, -3.86911],[43.46598, -3.86859],[43.46593, -3.86824],[43.46589, -3.86818],[43.46587, -3.86808],[43.46588, -3.86800],[43.46581, -3.86780],[43.46560, -3.86761],[43.46545, -3.86756],[43.46526, -3.86756],[43.46517, -3.86760],[43.46511, -3.86760],[43.46502, -3.86753],[43.46498, -3.86743],[43.46497, -3.86734],[43.46499, -3.86718],[43.46510, -3.86711],[43.46521, -3.86696],[43.46530, -3.86675],[43.46547, -3.86606],[43.46569, -3.86504],[43.46639, -3.86166],[43.46716, -3.86194],[43.46698, -3.86278]], 
"route_instructions": [["10","",56,0,155,"56m","SE",203.5],["7","",167,1,242,"167m","E",281.06],["3","Calle Polvorín",126,2,182,"126m","S",189.18],["7","CA-231",185,3,131,"185m","E",262.42],["11-2","CA-231",536,10,350,"536m","E",277.7],["11-1","CA-231",82,20,52,"82m","E",250.51],["11-2","Calle del Somo",36,31,19,"36m","NE",310.15],["1","Calle de El Somo",426,33,236,"426m","E",285.81],["7","Calle de Antonio Nebrija",88,36,127,"88m","N",17.56],["7","Calle de Manuel Cacicedo",70,37,76,"70m","W",103.84],["15","",0,38,0,"","N",0.0]], 
"route_summary": {"total_distance": 1890, 
        "total_time": 179, 
        "start_point": "", 
        "end_point": "Calle de Manuel Cacicedo"}, 
"via_points": [], 
"hint_data": {"checksum": -1013584035, 
       "locations": ["xqyjHgAAAACbAAAAzwAAABj5Tb5MZ9s_XFNCAG0U-v9", "WVr_FtzAKgAzAQAAaAAAAK5H_5Np-ec_SlNCABob-v9"]}, 
"transactionId": "OSRM Routing Engine JSON Descriptor (v0.3)"} 

但它是不可能得到該函數內部。我不知道會發生什麼。我試着寫document.location.href=作爲URL字符串中的另一個例子,但是這也失敗了。任何人都可以提出爲什麼這不起作用?

回答

0

您的網址應該是這樣的,

http://router.project-osrm.org/viaroute?rebuild=1&loc=43.46711564169348,-3.880102031707764&loc=43.4669443349282,-3.862788677215576&output=json 

,似乎不suppport JSONP回調,所以你不能用$ .getJSON的直接原因Cross-Origin Resource Sharing要求。

你應該使用一個代理來避免這個問題,例如,你可以使用YQL

here is an example with your url