简体   繁体   English

从本地服务器上的文件中读取XML或JSON

[英]Reading in XML or JSON from file on local server

I'm trying to load a list of coordinates for use in a google map from either an XML file or a JSON file (I have both hosted in same directory on local test server). 我正在尝试从XML文件或JSON文件(我都托管在本地测试服务器的同一目录中)中加载要在Google地图中使用的坐标列表。 I have used a hard-coded JSON object to load map coordinates to test so far. 到目前为止,我已使用硬编码的JSON对象加载地图坐标进行测试。 My question is, how do I replace that fake/hard-coded JSON with the XML/JSON file on my server? 我的问题是,如何用服务器上的XML / JSON文件替换该伪造/硬编码的JSON? Here's my code: 这是我的代码:

  (function() {

  // Creating an array that will contain all of our product icons
  var productIcons = [];

  productIcons['bulletin'] = new google.maps.MarkerImage(
    'icons/Bulletin-sm.png', 
    new google.maps.Size(48, 48), 
    null, 
    new google.maps.Point(24, 24)
  );

  productIcons['bus'] = new google.maps.MarkerImage(
    'icons/busicon.png', 
    new google.maps.Size(48, 48), 
    null, 
    new google.maps.Point(24, 24)
  );

  productIcons['bench'] = new google.maps.MarkerImage(
    'icons/BusBench-sm.png', 
    new google.maps.Size(48, 48), 
    null, 
    new google.maps.Point(24, 24)
  );

  productIcons['junior'] = new google.maps.MarkerImage(
    'icons/JuniorPoster-sm.png', 
    new google.maps.Size(48, 48), 
    null, 
    new google.maps.Point(24, 24)
  );

  productIcons['poster'] = new google.maps.MarkerImage(
    'icons/Poster-sm.png', 
    new google.maps.Size(48, 48), 
    null, 
    new google.maps.Point(24, 24)
  );


  productIcons['shelter'] = new google.maps.MarkerImage(
    'icons/TransitShelter-sm.png', 
    new google.maps.Size(48, 48), 
    null, 
    new google.maps.Point(24, 24)
  );

  window.onload = function() {

    // Creating a map
    var options = {  
      zoom: 3,  
      center: new google.maps.LatLng(37.09, -95.71),  
      mapTypeId: google.maps.MapTypeId.ROADMAP  
    };  
    var map = new google.maps.Map(document.getElementById('map'), options);  


// Creating a JSON object with fake product data (to be replaced by XML or JSON)
var productData = {'product': [
{
'lat': 40.756054,
'lng': -119.986951,
'productType': 'junior'
},
{
'lat': 35.620973,
'lng': -121.347276,
'productType': 'shelter'
},
{
'lat': 40.620973,
'lng': -121.347276,
'productType': 'bus'
},
{
'lat': 39.120973,
'lng': -122.847276,
'productType': 'bench'
},
{
'lat': 35.920973,
'lng': -122.347276,
'productType': 'bulletin'
},
{
'lat': 37.775206,
'lng': -122.419209,
'productType': 'poster'
}
]};
// Looping through the product array in productData
for (var i = 0; i < productData.product.length; i++) {
// creating a variable that will hold the current product object
var product = productData.product[i];
// Creating marker
var marker = new google.maps.Marker({
position: new google.maps.LatLng(product.lat, product.lng),
map: map,
icon: productIcons[product.productType]
});

}
}
})();

I'm thinking you could possibly setup a RESTFUL web service on your server and then use jquery's ajax capability to fetch the JSON content off from it. 我在想您可能可以在服务器上设置RESTFUL Web服务,然后使用jquery的ajax功能从中获取JSON内容。

A ajax tutorial: Ajax tutorial for post and get Ajax教程: 发布和获取的Ajax教程

As for the RESTFUL web service side, there are many technologies which you can explore. 至于RESTFUL Web服务方面,您可以探索许多技术。 Just search for "java restful web service tutorial" or ".NET WCF RESTFUL" 只需搜索“ Java Restful Web服务教程”或“ .NET WCF RESTFUL”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM