- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在调用 openweathermap 的网络服务,它在 map 上显示预报天气。
我正在解析其他标签,如天气、温度、风等。但是当我解析标签图标时,我在这个坐标上得到了错误:[weatherItem.coord.lon, weatherItem.coord.lat]。
这是我的代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Weather layer</title>
<style>
html, body{
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#map-canvas {
width: 100%;
height: 100%;
}
.gm-style-iw {
text-align: center;
}
</style>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js">
</script>
<script>
var map;
var geoJSON;
var request;
var gettingData = false;
var openWeatherMapKey = "57ba5be1b9a9d991f65909ee19523cc5"
function initialize() {
var mapOptions = {
zoom: 4,
center: new google.maps.LatLng(50,-50)
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
// Add interaction listeners to make weather requests
google.maps.event.addListener(map, 'idle', checkIfDataRequested);
// Sets up and populates the info window with details
map.data.addListener('click', function(event) {
infowindow.setContent(
"<img src=" + event.feature.getProperty("icon") + ">"
+ "<br /><strong>" + event.feature.getProperty("city") + "</strong>"
+ "<br />" + event.feature.getProperty("temperature") + "°C"
+ "<br />" + event.feature.getProperty("weather")
);
infowindow.setOptions({
position:{
lat: event.latLng.lat(),
lng: event.latLng.lng()
},
pixelOffset: {
width: 0,
height: -15
}
});
infowindow.open(map);
});
}
var checkIfDataRequested = function() {
// Stop extra requests being sent
while (gettingData === true) {
request.abort();
gettingData = false;
}
getCoords();
};
// Get the coordinates from the Map bounds
var getCoords = function() {
var bounds = map.getBounds();
var NE = bounds.getNorthEast();
var SW = bounds.getSouthWest();
getWeather(NE.lat(), NE.lng(), SW.lat(), SW.lng());
};
// Make the weather request
var getWeather = function(northLat, eastLng, southLat, westLng) {
gettingData = true;
//this below is openweathermapwebaservice which display forecast weather.
var requestString = "http://api.openweathermap.org/data/2.5/forecast?lat=&lon="
+ westLng + "," + northLat + "," //left top
+ eastLng + "," + southLat + "," //right bottom
+ map.getZoom()
+ "&cluster=yes&format=json"
+ "&APPID=" + openWeatherMapKey;
request = new XMLHttpRequest();
request.onload = proccessResults;
request.open("get", requestString, true);
request.send();
};
// Take the JSON results and proccess them
var proccessResults = function() {
console.log(this);
var results = JSON.parse(this.responseText);
if (results.list.length > 0) {
resetData();
for (var i = 0; i < results.list.length; i++) {
geoJSON.features.push(jsonToGeoJson(results.list[i]));
}
drawIcons(geoJSON);
}
};
var infowindow = new google.maps.InfoWindow();
// For each result that comes back, convert the data to geoJSON
var jsonToGeoJson = function (weatherItem) {
alert(jsonToGeoJson);
var feature = {
type: "Feature",
properties: {
city: weatherItem.name,
weather: weatherItem.weather[0].main,
temperature: weatherItem.main.temp,
min: weatherItem.main.temp_min,
max: weatherItem.main.temp_max,
humidity: weatherItem.main.humidity,
pressure: weatherItem.main.pressure,
windSpeed: weatherItem.wind.speed,
windDegrees: weatherItem.wind.deg,
windGust: weatherItem.wind.gust,
icon: "http://openweathermap.org/img/w/"
+ weatherItem.weather[0].icon + ".png",
coordinates: [weatherItem.coord.lon, weatherItem.coord.lat]
},
geometry: {
type: "Point",
coordinates: [weatherItem.coord.lon, weatherItem.coord.lat]
}
};
// Set the custom marker icon
map.data.setStyle(function(feature) {
return {
icon: {
url: feature.getProperty('icon'),
anchor: new google.maps.Point(25, 25)
}
};
});
// returns object
return feature;
};
// Add the markers to the map
var drawIcons = function (weather) {
map.data.addGeoJson(geoJSON);
// Set the flag to finished
gettingData = false;
};
// Clear data layer and geoJSON
var resetData = function () {
geoJSON = {
type: "FeatureCollection",
features: []
};
map.data.forEach(function(feature) {
map.data.remove(feature);
});
};
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>
最佳答案
如果你看一下JSON
来自您的天气 api 服务的响应,您将看到 coord
数据超出 list
数据,类似于 list
中所有数据的 header 数据.
因此,在调用 jsonToGeoJson
时函数,传给它results.city.coord
基础results.list[i]
参数:
geoJSON.features.push(jsonToGeoJson(results.list[i], results.city.coord));
更改 jsonToGeoJson
接受一个参数的签名:
var jsonToGeoJson = function (weatherItem, coord) {
里面jsonToGeoJson
函数,像这样访问坐标:
coordinates: [coord.lon, coord.lat]
关于javascript - TypeError : weatherItem. 坐标未定义 : coordinates: [weatherItem. coord.lon, weatherItem.coord.lat],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31403386/
我试过以下方法,输入:纬度/经度数据然后我会计算它周围的一个方框,比方说 50 米,所以东距/北距值 +/- 50 米。 现在我将它重新转换为纬度/经度并使用脚本: http://robotics.a
我正在使用 proj4 将纬度/对数坐标投影到 UTM。但我无法从 UTM 投影回 lat/log。对于以下示例,我希望代码返回给定的经纬度/对数 [48.37966, 2.504635]。相反,它返
我希望能够找到离经度/纬度元组最近的位置的经度/纬度坐标索引。这已经在 Java API 中作为 GridCoordSystem.findXYindexFromLatLon() 提供,但我还没有在
我是 python 新手,我正在尝试编写一个函数,该函数将从尺寸为 [时间、高度、经度、纬度] 的 netcdf 文件中获取 numpy 数组,并将函数插值到指定的纬度和经度。我已经研究过 scipy
我有一张包含邮政编码 (int) 和位置 (point) 的表格。我正在寻找 MySql 查询或函数。这是日期的示例。我想返回 100 英里。 37922|POINT(35.85802 -84.119
有谁知道是否有任何现有的解决方案可以从一个点(经度、纬度)确定时区? 我可以对这个项目所需的少数几个进行硬编码,但最好使用预构建的解决方案。 谢谢。 最佳答案 使用网络服务,例如 geonames 提
我正在构建一个小脚本,客户将在其页面上安装该脚本。地理定位对它来说不是必需的,但是如果它存在,那将是很好的。有没有办法让我检查客户端页面是否请求了地理位置信息,以及用户是否选择了允许获取纬度和经度而不
我有一组经纬度坐标,看起来像, 纬度=9339452,经度=4294611105 纬度=9386855,经度=4294690789 纬度=9388898,经度=4294697554 纬度=938943
我们正在开发一个现有网站,但无法重新构建数据库以进行更好的优化,因此不幸的是,我们正在运行大量“联接”来链接到每个表。 我们正在链接几个临时存储的邮政编码、用户帐户、体育事件、年龄组等的小表。 我不擅
我只是通过使用 [NSString stringWithFormat:@"%.5f, %.5f"]; 将 CLLocations 转换为逗号分隔的字符串,例如 "75.45874, -45.17292
我想为 Flask-Admin 创建一个 View 以在几何字段中输入坐标。如何创建两个文本字段并将它们转换为几何对象? 这是我目前为止尝试过的方法(除了不可数的其他事情) class CustomA
我想使用 Basemap 包将城市名称绘制到德国 map 上。我指定了经度和纬度值: Cname=Form_Cities["name"].values Clat=Form_Cities["lat
我正在尝试使用 Tweepy API 下载推文,但我无法在输出中获取地理坐标。 我正在寻找在输出数据中包含纬度和经度的方法。 感谢任何帮助..提前致谢。该代码是在 python 3.x 中开发的,输出
我正在尝试在 map 上放置一堆 (x,y) 点,它们的原点为经纬度。我想设置一个自定义的横向墨卡托投影,以我的原点为中心,并使用它将我的点投影到 lon-lat。 这似乎是 GeoToolkit(或
我如何访问用户的 iPhone 照片库并获取这些照片的经纬度信息? 谢谢 最佳答案 纬度/经度信息(如果有)将位于 JPG 文件的 EXIF 部分中。查看这个 exif iPhone 库: http:
我绞尽脑汁想不出如何构造这个 SQL 查询。 我有 3 张 table 。 用户表 用户 ID 姓名 图片等等 tag_ref表格 用户 ID tag_id 地理位置表 用户 ID geolat 吉隆
实体A有一个字段name 实体B有一个字段city 实体C有字段lat和lon 实体A与实体有一对多关联 B命名bees 实体B与实体有多对一关联 C命名cees 现在我想选择全部 A距离 Y 点 X
我有来自 GPS 的位置(lon_base,lat_base)。我有一个位置列表(lon1、lat1|lon2、lat2|lon3、lat3...)这个名单很长,遍布世界各地。 我的问题是:1. 如何
我想提取一个相当大的 netcdf 文件的空间子集。来自 Loop through netcdf files and run calculations - Python or R from pylab
我在导入具有 SRID 4326 的 wkt 多点特征的文件时遇到一些问题,其坐标是有序的(纬度、经度): >st_crs(4326) Coordinate Reference System: U
我是一名优秀的程序员,十分优秀!