Skip to content
Advertisement

Dynamically inserting latitude/longitude from array to weather API call in PHP

I have a leaflet map which has markers displaying the top 10 cities in a country depending on what country is chosen from a select field.

$latLng contains 10 latitude/longitude pairs and is being used to add each city at that location onto the map. Example in console (Australia):

(2) [-35.2834624726481, 149.128074645996]
(2) [-33.8678499639382, 151.207323074341]
(2) [-37.8139965641595, 144.963322877884]
(2) [-31.95224, 115.861397]
(2) [-34.928661, 138.598633]
(2) [-32.92953, 151.7801]
(2) [-42.87936056387943, 147.32940673828125]
(2) [-19.26639, 146.805695]
(2) [-16.92366, 145.76613]
(2) [-12.46113366159021, 130.84184646606445]

The locationList array is stringified and used as data for the AJAX call which is then decoded in PHP in a foreach loop – example of first pair: {"lat":-35.283462472648096763805369846522808074951171875,"lng":149.128074645996008484871708787977695465087890625}

In the PHP file I’m trying to figure out how to dynamically add $lat and $lng to the API routine for open weather so that when a particular $cityMarker is clicked, the weather forecast for that lat/lng appears in a modal.

I’ve tried adding a foreach loop in PHP to loop through all pairs for the weather cURL routine but at the moment I am only seeing the modal display weather for the final lat/lng pair in the array – [-12.46113366159021, 130.84184646606445] in the above case. Also, the modal only appears when the marker at the above location is clicked – clicking the other city markers only shows their leaflet popup.

Is there a better way to loop over all ten pairs so that the latitude/longitude of the marker that was clicked is matched and used in the weather API call? Or an alternative method for this?

Thanks for all the help!

PHP:

<?php

    ini_set('display_errors', 'On');
    error_reporting(E_ALL);

    $executionStartTime = microtime(true) / 1000;
    
    $locationList = json_decode($_POST['locationList'], true);

    $locationArray = [];
    
    foreach ($locationList as $location){
        $data['lat'] = $location['lat'];
        $data['lng'] = $location['lng'];
        array_push($locationArray, $data);
    }

    // openweather routine

    foreach ($locationArray as $location){
        $lat = $location['lat'];
        $lng = $location['lng'];

        $openWeatherUrl='api.openweathermap.org/data/2.5/weather?lat=' . $lat . '&lon='  . $lng  . '&units=metric&appid=demo';
    }

    $openWeatherch = curl_init();
    curl_setopt($openWeatherch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($openWeatherch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($openWeatherch, CURLOPT_URL,$openWeatherUrl);

    $openWeatherResult = curl_exec($openWeatherch);

    curl_close($openWeatherch);

    $openWeather = json_decode($openWeatherResult, true);

    $output['status']['code'] = "200";
    $output['status']['name'] = "ok";
    $output['status']['description'] = "mission saved";
    $output['status']['returnedIn'] = (microtime(true) - $executionStartTime) / 1000 . " ms";
    $output['data']['location'] = $locationArray;
    $output['data']['openWeather'] = $openWeather;
    

    header('Content-Type: application/json; charset=UTF-8');

    echo json_encode($output);

?>

Advertisement

Answer

Not tested, but you want to build the data portion in the loop. I changed $locationArray to $location in the ['data']['location'] portion:

foreach ($locationArray as $location){
    $lat = $location['lat'];
    $lng = $location['lng'];
    $openWeatherUrl='api.openweathermap.org/data/2.5/weather?lat=' . $lat . '&lon='  . $lng  . '&units=metric&appid=demo';

    $openWeatherch = curl_init();
    curl_setopt($openWeatherch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($openWeatherch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($openWeatherch, CURLOPT_URL,$openWeatherUrl);
    $openWeatherResult = curl_exec($openWeatherch);
    curl_close($openWeatherch);

    $openWeather = json_decode($openWeatherResult, true);
    $output['data'][] = ['location' => $location, 'openWeather' => $openWeather];
}   
$output['status']['code'] = "200";
$output['status']['name'] = "ok";
$output['status']['description'] = "mission saved";
$output['status']['returnedIn'] = (microtime(true) - $executionStartTime) / 1000 . " ms";

header('Content-Type: application/json; charset=UTF-8');
echo json_encode($output);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement