0

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);
  • Mostly this is something with the url or with permissions on php. Take a look into network tab in the developer tools and look if `php/reverseGeo.php` is resolved – Falke Design Oct 24 '20 at 10:47
  • php/reverse Geo.php status code - 200 php/countryList.php status code - 200 – Grzegorz Michniewicz Oct 24 '20 at 12:32
  • hmm ok this looks good, what is the response of the call? – Falke Design Oct 24 '20 at 12:36
  • countryList is sending an empty object instead of my geoJson feature on apache the same code is sending back the feature. This makes me lost. "Failed to load response" – Grzegorz Michniewicz Oct 24 '20 at 12:38
  • Ok now we know that the problem is that you don't get a response. Can you please [activate Error log in PHP](https://stackoverflow.com/a/21429652/8283938), then we see if the php has a problem. and is the php code the code what you posted – Falke Design Oct 24 '20 at 12:47
  • I didn't had the file you were right. I copied the whole folder for some reason that one file didn't make it. I had no reason to suspect that so never thought even to check that. Silly thing that made me struggle for very long. Big thanks! – Grzegorz Michniewicz Oct 24 '20 at 12:56

1 Answers1

0

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.

Falke Design
  • 7,419
  • 3
  • 8
  • 18