Accessing data stored in XML or JSON files from a local server

I am currently working on loading a list of coordinates for a Google map from either an XML or JSON file, both of which are hosted in the same directory on my local test server. So far, I have used a hard-coded JSON object to load map coordinates for testing purposes. My main query is: how can I replace that fake/hard-coded JSON with the actual XML/JSON file on my server? Below is the code I am using:

  (function() {

  // Array to store all 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);  


// Hard-coded 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]
});

}
}
})();

Answer №1

One option could be to set up a RESTful web service on your server and then utilize jquery's ajax function to retrieve the JSON data from it.

If you're looking for an ajax tutorial, check out this resource: Ajax tutorial for post and get

When it comes to implementing the RESTful web service, there are various technologies available for you to explore. You can start by searching for tutorials on "java restful web service" or ".NET WCF RESTFUL."

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Loading Bootstrap accordion content with AJAX only when clicked

Is there a way to load ajax content into an accordion only when it is active? This could help prevent unnecessary data loading. It would be helpful to have a spinner displayed while the content is loading. I've searched online but haven't found a ...

When the key code is equal to "enter" (13), the form will be submitted

When I press the Enter key, I want to submit a form if there are no error messages present. Here is the function I have created: $(targetFormID).submit(function (e) { var errorMessage = getErrorMessage(targetDiv); if (e.keyCode == 13 && errorMessa ...

Generate multiple divs or spans based on the JSON value in AngularJS

If I have a JSON file like this: { "count":"3" } How can I display 3 spans or any number of spans based on the value in the JSON? You can check out my Plunker demo here. ...

Unable to retrieve properties post JSON deserialization into dynamic

Struggling with accessing dynamic properties post JSON deserialization. Take a look at the JSON data: { "user" : { "511221" :{ "timestamp" : 1403365646384, "version" : -81317051, "email" : "<a href="/cdn-cgi/l/email-p ...

Promise chain failure in express

I've developed an express.js server for managing REST API requests and extracting data from a MongoDB database. However, I'm encountering an issue with the promise chain when I send a GET request to a specific endpoint ("localhost:8081/api/getUse ...

By default, the text area element in Vue or Nuxt will send a message to the console

Every time I include a textarea html element in my Vue/cli or Nuxt projects, this message appears in the console. Is there a way to prevent this message from showing up in the console? <textarea rows="5" cols="33"> This is a textarea. < ...

Simply modifying a custom attribute source using jQuery does not result in it being refreshed

Introduction: Utilizing jQuery.elevateZoom-3.0.8 to display zoomed-in images. The SRC attribute contains the path to the smaller/normal image The DATA-ZOOM-IMAGE attribute holds the path to the larger image used for zooming. Here is the HTML Code: ...

convert json information into individual lines within a csv document

I have a CSV file as well as a JSON file with multiple lines structured like this: { "name":"John", "age": {"f": "1", "f2": "2"}, "city":"New York"} { "name" ...

Get a document from a NodeJS Server with the help of Express

How can I improve my file downloading functionality in Node.js? Currently, when I try to download a PDF file from the server, the content is displayed as data instead of initiating the download process. I would like it to function similar to how it's ...

Difficulty paginating jqgrid when using an array as the data source

Encountering an issue with pagination in jqgrid while working with array data containing 18 records. Even after specifying pagination:true, pager:jQuery('#pager1'), the records are not displaying in pages. Can anyone assist me in implementing pag ...

What sets apart Object.assign {} from Object.assign []?

While reviewing code done by a previous developer who is no longer with us, I observed that they sometimes used Object.assign({}, xyz) and other times they used Object.assign([], abc); Could there be a distinction between the two methods? ...

How can I utilize the replace function in Angular to swap out a character?

I am attempting to change the characters { and } in an HTML string to {{ and }}. However, when using the replace function, I encounter the following error: $scope.selectedtemplate.template.replace is not a function Below is my code: $scope.selectedte ...

Using Node.js and Typescript to implement a chain of logical AND operations with an array of any length

Setting: I am developing a versatile function that determines a boolean outcome based on logical AND operations. However, the function is designed to handle various types of objects and arrays, each requiring different conditions. Currently, my code look ...

Tips for adjusting image dimensions with url() method in css

I have successfully incorporated collapsible functionality on my page. I would like to display a down arrow image instead of the default '+' symbol on the right side of the collapsible section. To achieve this, I can use the following CSS code s ...

Exploring the Power of D3.js: Loading and Visualizing Multiple Networks using JSON Files and the Force

I have a collection of networks consisting of nodes and links stored in various JSON files. I am using D3.js to load them into a Force Layout, where they each load and layout perfectly when loaded individually. However, I would like to enhance the function ...

Effect fails to activate on the third occurrence of the action

After successfully running on the first two action dispatches, the effects fail to trigger on the third time. I have tried the solutions provided in this thread and here, but none of them work for me. Here is the relevant code snippet: @Effect() get ...

Formatting numbers as floating point values in AngularJS

I need a text box where users can enter an amount. The input should be restricted to numbers only, with no special characters or decimal points. I have managed this using custom filters. However, I also need the entered number to automatically display as ...

Send a value to several PHP pages simultaneously using JQuery

I find myself asking this question due to my lack of experience. Is it possible to send a value to multiple PHP pages using JQuery? Let me illustrate what I am attempting to achieve. $(function() { $("#account").change(function() { $("#facilities" ...

"Encountering 'undefined' error in React JS console when accessing

I'm attempting to print the following: console.log(location_list.all_list[3]); console.log(location_list.all_list); However, I am getting "undefined" in one case. What could be causing this issue? https://i.sstatic.net/Xy0ge.png Additional code s ...

Guy discussing the ins and outs of JavaScript variables

I am facing an issue with retrieving the value in the jQuery script. The value should be taken from route_path_images, but it's not showing up. var route_path_images="http://www.domain.com" jQuery("#header_background_night_star").fadeIn(2000).css("b ...