Perry
January 15, 2019, 9:24pm
1
I would like to integrate an OSM map into my page to generate the bounding box I have to convert a number field value into a number.
I tried the following but that output is 0.
echo $lon = (int)(string)$page->location_lon();
How should I do it right?
The link target in this article no longer exists.
That’s a limitation of PHP. Objects can only be automatically converted to strings (using the __toString() method), not to other data types.
You can use the following though (a string can easily be converted to an int):
echo (int)(string)$page->my_field();
But yes, using ->value() is easier to understand and the way to go.
https://getkirby.com/docs/cheatsheet/field-methods/int:
Would it be possible to leave the old docs online for a while?
greetings perry
The K2 docs are still online : Kirby 2 Docs
Perry
January 15, 2019, 9:45pm
3
@bvdputte good to know but the links into the forum articles -> 404.
I get 0 because the string is e.g. 7,230456 ?
texnixe
January 15, 2019, 10:17pm
4
You don’t want to convert “7,xxxx” to integer, that doesn’t make sense. Also, if you have a string with a comma, you would have to replace that with a dot or use number_format()
to convert.
Perry
January 15, 2019, 10:21pm
5
Now I understand.
echo $lon = (float)(string)$page->location_lon();
thank you for your help.
texnixe
January 15, 2019, 10:24pm
7
@distantnative : I think float()
doesn’t really work with a decimal with a comma, does it? At least I tested this earlier today in K3, and I think the behavior is not different in K2.
If this works, ->float()
(using floatval
internally) should as well, don’t you think?
texnixe
January 15, 2019, 10:31pm
9
With
<?= $page->lon()->float() ?>
I only get a “7” from “7,123456” Works fine if the value is “7.123456”
As texnixe said, $page->location_lon()->float()
won’t work if location_lon()
contains a string with a comma.
Exactly the same happens when casting to (float)
.
Luckily the Kirby number field saves numbers with a dot, and not with a comma
Perry
January 16, 2019, 8:57am
11
yes! thank you for your help.
In the imput field (panel) a point is converted to the comma, so I thought it was also saved as a comma…
my osm snippet maybe someone needs it
<?php
$lon = (float)(string)$page->location_lon();
$lat = (float)(string)$page->location_lat();
//bounding box
//left
$bb_max_lon = $lon - 4.15;
//bottom
$bb_min_lat = $lat - 4.15;
//right
$bb_min_lon = $lon + 4.15;
//top
$bb_max_lat = $lat + 4.15;
echo "<iframe src='https://www.openstreetmap.org/export/embed.html?bbox=".$bb_max_lon.",".$bb_min_lat.",".$bb_min_lon.",".$bb_max_lat."&layer=mapnik&&marker=".$lat .",".$lon."'></iframe>";
?>