HTML-OSM
view release on metacpan or search on metacpan
lib/HTML/OSM.pm view on Meta::CPAN
zoom => 10,
);
Creates a new C<HTML::OSM> object with the provided coordinates and optional zoom level.
=over 4
=item * C<cache>
A caching object.
If not provided,
an in-memory cache is created with a default expiration of one hour.
=item * C<coordinates>
An array reference containing a list of coordinates.
Each entry should be an array with latitude, longitude, and an optional label, in the format:
[latitude, longitude, label, icon_url]
If latitude and/or longitude is undefined,
the label is taken to be a location to be added.
If no coordinates are provided, an error will be thrown.
=item * C<config_file>
Points to a configuration file which contains the parameters to C<new()>.
The file can be in any common format,
including C<YAML>, C<XML>, and C<INI>.
This allows the parameters to be set at run time.
=item * C<css_url>
Location of the CSS, default L<https://unpkg.com/leaflet@1.9.4/dist/leaflet.css>.
=item * C<geocoder>
An optional geocoder object such as L<Geo::Coder::List> or L<Geo::Coder::Free>.
=item * C<height>
Height (in pixels or using your own unit), the default is 400px.
=item * C<js_url>
Location of the JavaScript, default L<https://unpkg.com/leaflet@1.9.4/dist/leaflet.js>.
=item * C<min_interval>
Minimum number of seconds to wait between API requests.
Defaults to C<0> (no delay).
Use this option to enforce rate-limiting.
=item * C<ua>
An object to use for HTTP requests.
If not provided, a default user agent is created.
=item * C<host>
The API host endpoint.
Defaults to L<https://nominatim.openstreetmap.org/search>.
=item * C<width>
Width (in pixels or using your own unit), the default is 600px.
=item * zoom
An optional zoom level for the map, with a default value of 12.
=back
=cut
sub new
{
my $class = shift;
# Handle hash or hashref arguments
my $params = Params::Get::get_params(undef, \@_) || {};
if(!defined($class)) {
if((scalar keys %{$params}) > 0) {
# Using HTML::OSM:new(), not HTML::OSM->new()
carp(__PACKAGE__, ' use ->new() not ::new() to instantiate');
return;
}
# FIXME: this only works when no arguments are given
$class = __PACKAGE__;
} elsif(Scalar::Util::blessed($class)) {
# If $class is an object, clone it with new arguments
return bless { %{$class}, %{$params} }, ref($class);
}
if($params->{'coordinates'} && !ref($params->{'coordinates'})) {
Carp::croak(__PACKAGE__, ': coordinates must be a reference to an array');
}
$params = Object::Configure::configure($class, $params);
# Set up caching (default to an in-memory cache if none provided)
my $cache = $params->{cache} || CHI->new(
driver => 'Memory',
global => 1,
expires_in => '1 day',
);
# Set up rate-limiting: minimum interval between requests (in seconds)
my $min_interval = $params->{min_interval} || 0; # default: no delay
return bless {
cache => $cache,
coordinates => $params->{coordinates} || [],
height => $params->{'height'} || '400px',
host => $params->{'host'} || 'nominatim.openstreetmap.org/search',
width => $params->{'width'} || '600px',
zoom => $params->{zoom} || 12,
min_interval => $min_interval,
last_request => 0, # Initialize last_request timestamp
( run in 0.617 second using v1.01-cache-2.11-cpan-39bf76dae61 )