Show one marker on the map from a GeoJson file

On my webpage, I have a Google map that displays all markers using the map.data.loadGeoJson method.

Each marker is linked to its respective details page with the following code:

map.data.addListener('click', function(event) { 
    var id = event.feature.getProperty('name');
    window.location.href = 'submission-details.php?submission_id='+id;
    });

(The 'name' property in the json file serves as the ID for each submission).

Now, I want the details page to also show a map, but only with one marker specific to that particular submission.

Since my json type is feature collection, I believe I need a way to target individual features from the collection instead of using map.data.loadGeoJson, which shows all markers at once.

I'm unsure about the next steps and haven't been able to find similar queries. Any assistance would be greatly appreciated!

UPDATE Thank you, Duncan.

I have added the following:

      map.data.loadGeoJson('../photo_share/upload/file.json', {},function() {
      map.data.getFeatureById(53);
      map.data.getFeatureById(53).getProperty('name');
      console.log(map.data.getFeatureById(53));
      console.log(map.data.getFeatureById(53).getProperty('name'));
    });
google.maps.event.addDomListener(window, "load", initMap3);

While this displays in the console log, I am still unsure how to use it to show only the specific marker, as all markers are currently being displayed.

Answer №1

The google.maps.Data class provides a method called getFeatureById which allows you to retrieve a specific feature by its ID when passing through the submission-details.php page.

Make sure to use the loadGeoJson function and wait for it to finish loading before attempting to access the data. For more information, refer to this discussion on Stack Overflow: Google Maps API: getFeatureById for feature collection not working

Update: To prevent the entire collection from displaying, you might try implementing a solution like the one below (note that this code snippet is untested):

map.data.loadGeoJson('../photo_share/upload/file.json', {}, function(features) {
    // retrieve the desired feature:
    var feature = map.data.getFeatureById(53);

    // hide the current set of features:
    map.data.setMap(null);

    // remove all existing features:
    for (var i = 0; i < features.length; i++) {
        map.data.remove(features[i]);
    }

    // add the desired feature back in:
    map.data.add(feature);

    // display the updated feature:
    map.data.setMap(map);
});

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

Parsing URLs with Node.js

Currently, I am attempting to parse the URL within a Node.js environment. However, I am encountering issues with receiving null values from the following code. While the path value is being received successfully, the host and protocol values are returnin ...

Looking for a way to transfer the value of a variable to a PHP variable using any script or code in PHP prior to submitting a form?

Within this form, the script dynamically updates the module dropdown list based on the selected project from the dropdown box. The value of the module list is captured in a text field with id='mm', and an alert box displays the value after each s ...

Having trouble initializing the canvas with fabric.js and Vue.js, as the function this.lowerCanvasEl.getContext is not recognized

When attempting to initialize a canvas in a VueJS component, I encountered an issue. Here is an example: https://jsfiddle.net/eywraw8t/55338/ I tried initializing the canvas in the mounted hook, ensuring that the DOM is available. Fabric seems to be worki ...

Axios is passing an array instead of a JSON object when making a POST request

I am trying to make a post request using axios in my Vue.js front-end to communicate with Laravel on the backend. const data = { file: {id} } axios.post('api/documents/remove', data).then((response) => { ...

Contrasting the uses of element(...) versus element(...).getWebElement() in Protractor

What is the advantage of using element(...).getWebElement() instead of element(...) when they both perform similarly? Why are there two different APIs for the same purpose? ...

Having trouble muting the audio on my Vue audio player

I'm facing some challenges with muting the audio within my vue app. I have a list of songs that can be played, paused, shuffled, etc., but I can't seem to get the mute function working. Here's what I have in the JavaScript: mute() ...

Exploring the benefits of event subscription nesting

One feature I'm currently utilizing is the Angular dragula drag and drop functionality, which enables me to effortlessly move around Bootstrap cards within the view. When an item is "dropped," it triggers the this.dragulaService.drop.subscribe() funct ...

Modifying the user interface (UI) through the storage of data in a class variable has proven to be

If I need to update my UI, I can directly pass the data like this: Using HTML Template <li *ngFor="let post of posts; let i = index;"> {{i+1}}) {{post.name}} <button (click)="editCategory(post)" class="btn btn-danger btn-sm">Edit</butto ...

A guide on integrating a Google Maps API_KEY with angular-google-maps 2.2.1

In the midst of my current mobile app project, I am dealing with an application that was created back in 2016 and currently has around 4500 users. The app was built using AngularJS (v 1.7.8) and IONIC (V1.1.1), and we are required to stick with these older ...

The two-word string is failing to pass through the query string

I've been working on passing multiple values through the query string. So far, I have successfully passed single-word values to a PHP page using jQuery. However, I encountered an issue when trying to pass a user's name like "Vishal Deb" as it onl ...

turn on labels for every individual cell in the bar graph

I'm working on setting labels of A, B, and C for each bar chart cell to display all my data on the chart. I attempted using data.addColumn('string', 'Alphabets'); but it's not functioning as expected. It seems like a simple ta ...

What could be causing the issue with dayjs dynamic importing in TypeScript?

Currently, I am developing a web screen within a .NET application and facing an issue with sending datetime preferences from the system to the web screen using CefSharp settings. AcceptLanguageList = CultureInfo.CurrentUICulture.Name In my TypeScript code ...

Is there a way to automatically fill number fields by clicking on radio buttons using a JavaScript function?

Currently, I am facing an issue with a task that involves three elements: 1. Two Number fields (from standard class) that need to be populated dynamically. 2. A Javascript function designed to extract coordinates using geolocator. 3. A radio button which, ...

The resource in CosmosDB cannot be found

I have successfully stored documents on Cosmos, but I am encountering an issue when trying to retrieve them using the "read" method. this.cosmos = new CosmosClient({ endpoint: '' key: '' }); this.partitionKey = '/id' thi ...

Enhance your website with the jQuery autocomplete feature, complete with

Is there a way to incorporate smaller text descriptions alongside the search results displayed on my website? The descriptions are available in the data array used by autocomplete and can be accessed using the .result function by calling item.description. ...

Address the snack bar problem

In my quest to create a custom snackbar, I have encountered a minor issue with setting and deleting session variables in Node.js. While using global or local variables works well for accessing data on the client side, there is a chance of issues when multi ...

Fetching JSON Data in PHP and converting it to regional Language

My MYSQL database is set to collation utf16le_general_ci to support my regional language (GUJARATI), and I am successfully storing data in that language. However, when I use PHP (version 7+) to retrieve and echo the data in JSON format, I encounter this i ...

Unable to eliminate the default styling of Material UI table using an external CSS file

Currently, I am incorporating a Material Ui table into my project. My goal is to eliminate the border and adjust the padding of the table. Upon investigation, I came across a default className for material ui table known as MuiTableCell-root-40. Below is t ...

jquery toggle for partial visibility not functioning properly

Can jquery sliding functions be used to partially show and hide content? In this scenario, there is a list with 7 items but only the first two are visible initially, while the rest are hidden. The goal is to have all 7 items show when the user clicks to v ...

Utilizing AngularJS: Techniques for Binding Data from Dynamic URLs

Just starting out with AngularJS We are using REST APIs to access our data /shop/2/mum This URL returns JSON which can be bound like so: function ec2controller($scope, $http){ $http.get("/shop/2/mum") .success(function(data){ ...