在PHP中动态地从数组插入纬度/经度到天气API调用



我有一个传单地图,其中有标记显示一个国家的前10个城市,这取决于从选择字段中选择了哪个国家。

$latLng包含10个纬度/经度对,用于将该位置的每个城市添加到地图上。控制台示例(澳大利亚):

(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]

locationList数组被字符串化并用作AJAX调用的数据,然后在PHP中在foreach循环中解码-第一对的例子:{"lat":-35.283462472648096763805369846522808074951171875,"lng":149.128074645996008484871708787977695465087890625}

在PHP文件中,我试图找出如何动态地将$lat$lng添加到开放天气的API例程中,以便当单击特定的$cityMarker时,该天气预报出现在模式中。

我已经尝试在PHP中添加一个foreach循环来循环所有对天气cURL例程,但目前我只看到数组中最后的lat/lng对的模态显示天气-在上述情况下[-12.46113366159021, 130.84184646606445]。此外,模态只有在点击上述位置的标记时才会出现——点击其他城市标记只会显示它们的传单弹出。

是否有更好的方法来遍历所有十对,以便点击的标记的纬度/经度匹配并在天气API调用中使用?或者另一种方法?

谢谢你的帮助!

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);
?>

没有经过测试,但是您希望在循环中构建data部分。我在['data']['location']部分将$locationArray更改为$location:

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);

最新更新