从数组动态插入 latitude/longitude 到天气 API 调用 PHP
Dynamically inserting latitude/longitude from array to weather API call in PHP
我有一张传单地图,上面有标记显示一个国家/地区的前 10 个城市,具体取决于从 select 字段中选择的国家/地区。
$latLng
包含 10 个 latitude/longitude 对,用于将该位置的每个城市添加到地图上。控制台中的示例(澳大利亚):
(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 调用的数据,然后在 foreach 循环中的 PHP 中解码 - 第一对示例:
{"lat":-35.283462472648096763805369846522808074951171875,"lng":149.128074645996008484871708787977695465087890625}
在 PHP 文件中,我试图弄清楚如何将 $lat
和 $lng
动态添加到开放天气的 API 例程中,以便当单击特定 $cityMarker
,该 lat/lng 的天气预报将出现在模式中。
我尝试在 PHP 中添加一个 foreach 循环来遍历天气 cURL 例程的所有对,但目前我只看到最后 lat/lng 对的模式显示天气在数组中 - [-12.46113366159021, 130.84184646606445]
在上面的例子中。此外,模态仅在单击上述位置的标记时出现 - 单击其他城市标记仅显示其传单弹出窗口。
是否有更好的方法来遍历所有十对,以便在天气 API 调用中匹配并使用被单击的标记的 latitude/longitude?还是另一种方法?
感谢大家的帮助!
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);
我有一张传单地图,上面有标记显示一个国家/地区的前 10 个城市,具体取决于从 select 字段中选择的国家/地区。
$latLng
包含 10 个 latitude/longitude 对,用于将该位置的每个城市添加到地图上。控制台中的示例(澳大利亚):
(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 调用的数据,然后在 foreach 循环中的 PHP 中解码 - 第一对示例:
{"lat":-35.283462472648096763805369846522808074951171875,"lng":149.128074645996008484871708787977695465087890625}
在 PHP 文件中,我试图弄清楚如何将 $lat
和 $lng
动态添加到开放天气的 API 例程中,以便当单击特定 $cityMarker
,该 lat/lng 的天气预报将出现在模式中。
我尝试在 PHP 中添加一个 foreach 循环来遍历天气 cURL 例程的所有对,但目前我只看到最后 lat/lng 对的模式显示天气在数组中 - [-12.46113366159021, 130.84184646606445]
在上面的例子中。此外,模态仅在单击上述位置的标记时出现 - 单击其他城市标记仅显示其传单弹出窗口。
是否有更好的方法来遍历所有十对,以便在天气 API 调用中匹配并使用被单击的标记的 latitude/longitude?还是另一种方法?
感谢大家的帮助!
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);