Converting (Geo)JSON data into an array of objects

I am new to JavaScript and facing some difficulties in converting GeoJSON to a JavaScript Object Array.

Using JSON.parse, I successfully parse the JSON generated on the server into a JSON Object.

The addGeoJson method from Google returns [object (Array)], displaying the shape on the map immediately. However, I require it later for different granularities when zooming in or out.

How can I generate an [object (Array)] from an [object]? Specifically, how do I do this with "generatedGeoJsonPlz2"?

This is my current progress:

var generatedGeoJsonPlz1 = JSON.parse(geoJsonString); 
var generatedGeoJsonPlz2 = JSON.parse(geoJsonString2);

var plz1area = plz.data.addGeoJson(generatedGeoJsonPlz1); // returns [object (Array)]

plz.addListener('zoom_changed', function() {
  var zoom = plz.getZoom();
  if (zoom == 8) {
    for (var i = 0; i < plz1area.length; i++) {
      plz.data.remove(plz1area[i]);
    }
    plz.data.addGeoJson(generatedGeoJsonPlz2);
  }
  else if (zoom == 5) {
    var plz2area = generatedGeoJsonPlz2;

    for (var i = 0; i < plz2area.length; i++) {
      plz.data.remove(plz2area[i]);
    }
    plz.data.addGeoJson(generatedGeoJsonPlz1);
  }
}

Thank you! Christian

Answer №1

If you want to retain the ability to remove the result of addGeoJson(generatedGeoJsonPlz2) later on, you can store it for future reference.

Consider implementing the following approach:

var storedGeoJsonData1 = JSON.parse(geoJsonString); 
var storedGeoJsonData2 = JSON.parse(geoJsonString2);

var data1Area = plz.data.addGeoJson(storedGeoJsonData1);
// Defining the variable for data2Area
var data2Area;

plz.addListener('zoom_changed', function() {
  var zoomLevel = plz.getZoom();
  if (zoomLevel == 8) {
    for (var i = 0; i < data1Area.length; i++) {
      plz.data.remove(data1Area[i]);
    }
    data2Area = plz.data.addGeoJson(storedGeoJsonData2);
  } else if (zoomLevel == 5) {
    // Check if data2Area has been initialized before removing it from the map
    if (data2Area) {
      for (var i = 0; i < data2Area.length; i++) {
        plz.data.remove(data2Area[i]);
      }
    }
    plz.data.addGeoJson(storedGeoJsonData1);
  }
}

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

A guide to setting an href using variable values in jQuery through manual methods

I have a datepicker set up where each day, month, and year is stored in a variable. I then display this information in the desired format. Below is the jQuery code: jQuery(document).ready( function($){ alert('alert function'); var txtFr ...

IntelliJ is not able to detect certain JS libraries

Currently, I am working on an NDV3 AngularJS graph demo using IntelliJ. To start off, I have created a standard HTML file named index.html and included all the necessary AngularJS and NDV3 libraries in a folder called bower_components located within the r ...

What is the best method for adding anchors or appending items in aspx pages?

I have created code for an image gallery on my webpage, "gallery.aspx". The situation is that I have an external aspx page with various links. When a user clicks on one of these links, I want them to be redirected to the image gallery but to a specific ima ...

Fixing the 'Segmentation Fault (core dumped)' error like a pro

I have recently delved into learning C and decided to practice by writing a small program that involves using char arrays. The program is designed to take a string from stdin, eliminate trailing whitespaces and empty lines, inspired by problem 1-18 in the ...

Printing the object name in CreateJS can be achieved by accessing the name property

var stage = new createjs.Stage("demoCanvas"); console.log(stage.constructor.name);//prints a <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js></script> <script src="https://code.createjs.com/createjs-2015.11.26.mi ...

How do I go about developing a function in processing that can identify the maximum value in a given array?

Looking to create a function that can identify the maximum value in the volume array and return the corresponding note from the note array. Furthermore, I am interested in finding a way to adjust it so that only the loudest note is played. import arb.sou ...

Remove the initial x characters from a numerical value and verify if it aligns with a specified regular

I need to remove the initial 6 characters (numbers) from a number and verify if it matches any number on a given list. For instance, if we have a number input like: 1234567891234567 The first 6 characters extracted would be: 123456 Then I want to confi ...

Retrieve JSON data in camelCase format from a Web API

My goal is to retrieve camel cased JSON data from an ASP.Net Web API 2 controller. To achieve this, I set up a new web application specifically with the ASP.Net MVC and Web API components. I modified the ValuesController as shown below: public class Value ...

Extracting CSS data and storing it in a SQL database

Hello there, I've created a div using CSS like this: echo '#'. $pess2['naam'] .' { width: 190px; height: 90px; padding: 0.5em; float: left; left:'. $pess2['left'] .'px; top:'. $pess2['top'] ...

Perform the function with the value of the currently selected option

Despite selecting an option, a message still appears stating "Please choose something." This function works with only one dropdown list, but encounters issues when there are two or more. (In this example, the variable 'sport' is document.getElem ...

A guide to assigning multiple classes to an element in Vue.js depending on the props!

Take a look at my code to grasp the issue. <template> <div class="header" :class="flat ? 'flat' : null" :class="app ? 'app' : null"> </div> </template> <script> export default ...

Utilizing integer-based polymorphic deserialization instead of string-based approach in Jackson

Typically, when utilizing polymorphic deserialization with Jackson, I usually have a string field that corresponds to a particular class, and the implementation may look like this. @JsonTypeInfo( use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo. ...

Encountering difficulties in retrieving the JSON data from the web service

My external web service contains JSON data which has been validated using the JSON Validator website. However, I am facing an issue where the success function in the code below is not running at all. The web service necessitates that the email and password ...

What enables typescript to be eligible for transpiling is its equivalent level of abstraction to javascript?

Transpilation is the act of converting code from one language to another with a similar level of abstraction. Can you point out some distinctive characteristics that indicate TypeScript transpires into JavaScript? ...

Transform the jQuery UI Slider to be clickable rather than draggable

Currently using jQuery UI 1.10.3 and aiming to create a slider widget that is not draggable. The intention is for the user to click on specific values along the slider instead. I previously attempted using eventDefault in the slider's start method, b ...

Encountering a null object in my WCF service while attempting to incorporate a response format and body style

string baseAddress = "http://" + Environment.MachineName + ":8000/RestServiceImpl"; ServiceHost host = new ServiceHost(typeof(RestServiceImpl), new Uri(baseAddress)); //host.AddServiceEndpoint(typeof(IRestServiceImpl), new WebHttpBinding(), ...

Issues with HTML keycodes not functioning properly in Firefox

Here is the code I am using: function restrictInput(e) { var charCode = (e.which) ? e.which : ((e.charCode) ? e.charCode : ((e.keyCode) ? e.keyCode : 0)); if((charCode < 48 || charCode > 57) && ( ...

The multiArr.shift() operation is throwing a TypeError: The function shift cannot be located in the SN object

As a beginner in Java app development, I am currently attempting to export JSON from a Google spreadsheet. While watching a tutorial, I came across the following code: function doGet(){ var result ={}; var sheet1 = SpreadsheetApp.getActiveSpread ...

How to Parse a JSON Object with Variable Fields in C#

I have a json document that looks like this: { "name": "bert", "Bikes": { "Bike1": { "value": 1000, "type": "Trek" }, "Bike2": { "value&q ...

Displaying JSON data from multiple rows and parsing it into PHP tables using a primary key date

date , details { "target1" : "test1", "count1" : "3", "remarks1" : "done", "target2" : "test2", "count2" : "3", "remarks2" : "done", "target3" : "test3", "count3" : "3", "remarks3" : "running" } Two ...