Search code examples
javascriptgeolocationleaflet

Leaflet Bounds are not valid


So I'm trying to fitBounds on leaflet map to the desired geoJson feature. Firstly I'm returning latitude and longitude from navigator getCurrentPosition() method to use reverse geocoding and get the country name. Secondly the country name is used to extract geoJson feature from a geoJson file on my server. Then returned feature is the used to draw a polygon and fitBounds and everything works fine on apache. When I upload the code on my hosting service I'm getting an error that bounds are not valid. I've tried several approaches without success.

Any ides what I'm doing wrong? Anything to do with the hosting service?

// get current position

navigator.geolocation.getCurrentPosition(position => {
    let lat = position.coords.latitude;
    let lng = position.coords.longitude
   
    L.marker([lat, lng]).addTo(mymap);
// get current country
    $.post('php/reverseGeo.php', {
        lat: lat,
        lng: lng
    }, response => {
        let name = JSON.parse(response)
        let country = name['address']['country']
        $.post('php/countryList.php', {
            countryName: country
        }, result => {
            let decoded = JSON.parse(result)
            let border = L.geoJSON(decoded).addTo(mymap)

            console.log(border.getBounds())
            mymap.fitBounds(border.getBounds())
            $('#element1').slideDown()
            ...

When I log border.getBounds() to the console it returns nothing. On apache it return the correct object.

PHP routine filtering out specific feature:

<?php

$content = file_get_contents('../vendors/countries/countries_large.geo.json');

$decoded = json_decode($content, true);

$info = '';

$name = $_REQUEST['countryName'];

foreach ($decoded['features'] as $feature) {
    if ($feature['properties']['ADMIN'] == $name) {
        $info = $feature;
    break;
    } else {
        $info = 'Not supported';
    }
}

echo json_encode($info);

Solution

  • After checking the response of the requests in the network tab in the developer tools, the problem was that a file which was opened in the php code, was not on the server.