November 19, 2013 at 9:40 am #13790
I try to generate a map from custom fields, and display the map from php code in a content template.
I have no problem to get the value from the custom fields in my template. (These values are also used to generate some other content). I can’t understand why the map is not displaying at all. Any clue to help me ? Thanks in advance,
Here is basically my code :
$nom_resto = get_field('nom_resto'); // get the value of the custom field "nom_resto" $adresse_complete_resto_br = get_field('adresse_resto')."<br />". get_field('code_postal_resto') ." ". get_field('ville_resto'); // get the values from the address-related custom fields and concatenate to obtain full address. $mymap = new Mappress_Map(array("width" => 646)); $mypoi_1 = new Mappress_Poi(array("title" => $nom_resto, "body" => $nom_resto, "address" => $adresse_complete_resto_br )); $mymap->pois = array($mypoi_1); echo $mymap->display();November 19, 2013 at 10:25 am #13792
Can you provide a URL for your map?
I don’t think your POI has a location. You’ll need to geocode the address or provide a lat/lng in order to see a marker. See the documentation section ‘Geocoding with PHP’ if you need to geocode. Lat/lng can be done with:
$mypoi_1->point = array('lat' => 40, 'lng' => -74.1234);November 19, 2013 at 11:42 am #13793
Thank you for your quick answer.
I tried the function geocode() in my template like this :
$mymap = new Mappress_Map(array("width" => 646)); $mypoi_1 = new Mappress_Poi(array("address" => $adresse_complete_resto_br )); $mypoi_1 = geocode(); $mymap->pois = array($mypoi_1); echo $mymap->display();
and I get a PHP error ‘call to undefined function’.
Here is the URL: See at the bottom of the post, under the title ‘MapPress Test’
ThanksNovember 19, 2013 at 12:22 pm #13794
( sorry, I missed that on the above post) the url is : http://semiocode.com/tsbv2/nos-maisons/manoir-de-la-boulaie/)
Just made some tests, and get closer to the solution.
You were right, latitude and longitude were missing.
I tried with a POI containing the point parameter and it works. But the problem still remains to output latitude and longitude from the geocode() function. I need to dynamically generate latitude and longitude from address.
This code works:
$mymap = new Mappress_Map(array("width" => 646)); $mypoi_1 = new Mappress_Poi(array("address" => $adresse_complete_resto_br, "point" => array("lat" => 47.1913630, "lng" => -1.4291270) )); // the address parameter is not necessary $mymap->pois = array($mypoi_1); echo $mymap->display();
This code doesn’t work:
$mymap = new Mappress_Map(array("width" => 646)); $mypoi_1 = new Mappress_Poi(array("address" => $adresse_complete_resto_br )); $mypoi_1->geocode(); // Apparently not working $mymap->pois = array($mypoi_1); echo $mymap->display();November 19, 2013 at 12:39 pm #13795
Another odd thing :
If the “BiggerMap” link is not selected in the settings and thus is not displayed in the front-end, the map doesn’t show. If the link is here, I have to click on it to show the map.November 19, 2013 at 4:25 pm #13796
In your code, I don’t see you assigning a height. Also, have you checked the contents of $adresse_complete_resto_br? It may not be a valid address.
The geocoding function will return an instance if WP_Error if there’s an error, so you can check for that if needed.
Here’s an example that works for me:
<?php $mymap = new Mappress_Map(array("width" => 646, 'height' => 100)); $address = "30 chestnut street"; $mypoi_1 = new Mappress_Poi(array("address" => $address )); $mypoi_1->geocode(); // Works for me $mymap->pois = array($mypoi_1); echo $mymap->display(); ?>November 19, 2013 at 9:45 pm #13798
Hi Chris, thank you very much, now it works perfectly. Last question : You understood I generate maps dynamically for each post using my content template. Now I’d like to do a mashup of these, possible ?November 20, 2013 at 5:43 am #13801
That’s not possible because the maps generated dynamically aren’t saved.
I don’t know what you generate the maps from, but if you can generate them from custom fields in the post you can use the MapPress geocoding settings to automatically create a map for each post and save it. Then, the saved maps can be combined in a mashup normally.November 20, 2013 at 7:11 am #13802
Hi, yes a map is generated for each post. It is generated from custom fields, not for each post in the Back-office, but in the template called when posts are displayed.
eg. I have a template for posts called content.php
In the WordPress Back-office : To add a new post, you have to fill a form. Each field of the form is a custom field.
For the address, you have 3 custom fields : street address, postal code, city.
In the Front-end : When a post is displayed, it calls content.php
in content.php, there are some lines of code which generates the map from the custom field values. In order to correctly display the map, I’ve had to concatenate the 3 custom fields relating to the address.
I tried to generate the maps with the Geocoding settings, but it’s not working for me: I guess it’s because of the address-related custom fields. I tried with the custom field ‘street address’ in address line 1, ‘postal code’ in address line 2, and ‘city’ in address line 3.
Nonetheless the geocoding does not work, but with this method I also cannot control the exact place where the map displays in the post. The ‘before content’ or ‘after content’ options don’t fit with the other parts of the post’s content.November 20, 2013 at 8:22 am #13804
The custom field names will be whatever field was saved by the form. You can check the database table wp_postmeta for a post that was saved to find the correct names – they usually don’t have spaces in them.
If the address isn’t valid, or your server is over the Google geocoding limit, you won’t be able to geocode. In that case, the plugin will write a custom field called ‘mappress_errors’ with the error message in it.
The before/after options are a convenience. You can put the shortcode [mappress] anywhere in your post.
- You must be logged in to reply to this topic.