Add marker dynamically with PHP
-
Hey,
Is it possible to add marker dynamically with PHP? I want to display different stored locations depending on the filter selection of the user.
I’m currently using the free version but considering to upgrade to the pro version if my problem could be solved.
Best,
Steffen
-
Hi there
Thank you for getting in touch!
You can create markers in the database through
$wpdb, our latest version includes experimental CRUD classes, you can use WPGMZA\Marker to add, delete and manipulate markers. These classes aren’t in use at the moment but they do work well.My apologies that there is no documentation for this at the moment.
Does that answer your question?
Kind regards
– PerryThanks for the quick response.
The users can filter entries based on specific attributes in the front-end. Depending on the filter I want to display all markers on a map that match the filtered attribute. Therefore I need to dynamically create maps with changing markers based on the filtered attributes.
Could I realize that with Map Mashup? If I have an empty parent map and add all matching entriees with the mashup_ids.
echo do_shortcode('[wpgmza id="2" mashup=true mashup_ids=<matching entries> parent_id="2"]);Is there any better solution for this problem?
Hi there
Thanks for that,
I understand you want to filter and create dynamic maps, potentially using the mashup feature, but I’m not sure I quite follow.
Can you please describe in real, practical terms what the purpose of your application is?
I understand the technicalities, I may be able to offer better help if I can get some context 🙂
Kind regards
– PerryOkay, we want to display products in the woocommerce shop page. Every product has an attribute „address“ which has the value of the products address (It‘s more a bookable activity than a product). We could also add a lat and lng attribute that stores the location of the product.
The maximum products that are displayed on one page are 4. We want to show a map with the markers of the actually displayed products. The user can filter the products in the front-end. Therefore we have to customize the markers on the map based on the actually displayed products.
The only solution that came in my mind was to create a map for every single product and use the Map Mashup to display a parent map in combination with the map of the actually displayed products.
Hi there
We will be integrating with Woocommerce at some point in the future to address this, in the mean time the most optimal solution I can suggest would be the following:
– Set up a blank map on the page
– Set up some Javascript on the page in question to send an AJAX request
– Create an AJAX endpoint to receive a latitude and longitude
– On that endpoint, find the nearest 4 locations to the given latitude and longitude
– Return the title, address, latitude and longitude etc. to the Javascript module
– For each result returned, callWPGMZA.Marker.createInstance, passing in the data received from the serverI can give you code to do the last steps, I’d have to see your database first hand to offer help on querying the Woocommerce products.
I hope that helps?
Kind regards
– PerryPerfect, thanks. I will give it a try.
-
This reply was modified 7 years, 9 months ago by
weekendize.
Hey Perry,
I have now the title, address, latitude and longitude etc. available in the module. What parameter do I have to pass to the function WPGMZA.Marker.createInstance? Or could you provide a short code snipplet?
Thanks in advance.
Steffen
Hi Steffen
I’m glad to hear that’s working well!
WPGMZA.Marker.createInstance takes one parameter, which is a “settings” object.
Any data in there will be applied to the marker.
For each property on the object you pass in, if the marker has a property with the same name, it will be set there. Otherwise, it will be stored in the markers settings object.
For example:
var marker = WPGMZA.Marker.createInstance({ title: "Example marker", lat: 52, lng: -2, animation: WPGMZA.Marker.ANIMATION_BOUNCE, arbitraryProperty: "This will be stored on the settings object" });I hope that helps?
Kind regards
– PerryHi Perry,
sorry for the late reply, I was busy the last couple of weeks.
How can I set up a blank map on the page with JavaScript? With the function WPGMZA.Map.createInstance() or just use a shortcode? Is there a documentation where I can look up the necessary parameters?Thanks in advance.
SteffenHi Steffen
My apologies also for the delay in responding, I’ve been on leave for the last weke.
You can set up a blank map on a page using the shortcode, you could also technically use JavaScript, depending on your requirements.
Here is the documentation on our shortcode paramaters
I hope that helps?
Kind regards
– PerryThanks for your response.
How can I create an empty map with JavaScript? Do you provide any documentation/tutorial for your plugin regarding to JavaScript?Hi Steffen
We don’t provide any developer documentation at the moment, however we are working towards this.
You can create a map by creating a HTML element, and passing the element to WPGMZA.Map.createInstance like so
var map = WPGMZA.Map.createInstance( document.getElementById("example") );I hope that helps?
Kind regards
– PerryPerfect, thanks!
And how can I add the markers to the created map?Hi there
Please create a marker, passing the map as an option, or call
addMarkeron the map afterwards:var marker = WPGMZA.Marker.createInstance({ lat: 52, lng: -2 }); map.addMarker(marker);I hope that helps?
Kind regards
– PerryI get the error “Uncaught ReferenceError: WPGMZA is not defined” when I use the functions in my ‘functions.php’ or in a seperated js file. What file do I need to include in order to use the functions?
Thanks in advance.
-
This reply was modified 7 years, 9 months ago by
The topic ‘Add marker dynamically with PHP’ is closed to new replies.