gpt4 book ai didi

javascript - 从反向地理编码中提取城市

转载 作者:行者123 更新时间:2023-11-30 05:43:54 26 4
gpt4 key购买 nike

我正在努力根据纬度和经度查找城市名称。

我正在从来自 flickr 的 json 回调中解析纬度和经度。现在,我正在尝试对纬度和经度进行反向地理编码以确定图片是否在洛杉矶。我正在尝试计算在洛杉矶的照片和不在洛杉矶的照片。

当我将我的代码加载到浏览器中时,没有任何反应。我没有从 Firebug 那里得到任何错误。我在一个单独的 html 文件中测试了经度和纬度的 json 解析,所以我知道那部分工作正常。我能够显示每张图片的纬度和经度。我正在苦苦挣扎的是反向地理编码。

感谢您的帮助! :)

<!DOCTYPE html>
<html>
<head>
<title>Flickr Pie Chart</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<meta charset="utf-8">
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB3eu10Q0JajjUWTEQd_ENYDT64u3ZM7KA&sensor=false">
</script>
<script type="text/javascript">
var latitude;
var longitude;
var geocoder;
var city;
var cityName = "Los Angeles";
var inLosAngeles;
var notLosAngeles;
var map;
var infowindow = new google.maps.InfoWindow();
var marker;
var latlng;
var inside = 0;
var outside = 0;
$(document).ready(function() {
function extractFromAdress(components, type) {
for (var i = 0; i < components.length; i++)
for (var j = 0; j < components[i].types.length; j++)
if (components[i].types[j] == type) return components[i].long_name;
return "";
}
var url = 'http://api.flickr.com/services/rest/?&method=flickr.photos.search&api_key=14bfe578621f2dfe5c58086de5c79ab9&tags=losangeles&has_geo=1&extras=geo,tags&format=json&nojsoncallback=1';
$.getJSON(url, function(data) {
//loop through the results with the following function
$.each(data.photos.photo, function(i, item) {
latitude = item.latitude;
longitude = item.longitude;
geocoder = new google.maps.Geocoder();
latlng = new google.maps.LatLng(latitude, longitude);

function codeLatLng() {
var input = document.getElementById('latlng').value;
var latlngStr = input.split(',', 2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({
'latLng': latlng
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if(results[0]){

var town = extractFromAdress(results[0].address_components, "locality");
if (town == cityName) {
inside += 1;
} else {
outside += 1;
}
}
}
});
}
});
inLosAngeles = inside;
notLosAngeles = outside;
$('#results').append('inLosAngeles' + inLosAngeles + '' + 'notLosAngeles' + notLosAngeles);
});
});
</script>
</head>

<body>

<div id = "results"></div>
</body>

</html>

最佳答案

<!DOCTYPE html>
<html>
<head>
<title>Flickr Pie Chart</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<meta charset="utf-8">
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB3eu10Q0JajjUWTEQd_ENYDT64u3ZM7KA&sensor=false">
</script>
<script type="text/javascript">
var latitude;
var longitude;
var geocoder;
var city;
var cityName = "Los Angeles";
var inLosAngeles;
var notLosAngeles;
var map;
var infowindow = new google.maps.InfoWindow();
var marker;
var latlng;
var inside = 0;
var outside = 0;
$(document).ready(function() {
function extractFromAdress(components, type,type1) {
for (var i = 0; i < components.length; i++)
for (var j = 0; j < components[i].types.length; j++)
if ((components[i].types[j] == type) || (components[i].types[j] == type1)) return components[i].long_name;
return "";
}
var url = 'http://api.flickr.com/services/rest/?&method=flickr.photos.search&api_key=14bfe578621f2dfe5c58086de5c79ab9&tags=losangeles&has_geo=1&extras=geo,tags&format=json&nojsoncallback=1';
$.getJSON(url, function(data) {
//loop through the results with the following function
$.each(data.photos.photo, function(i, item) {
latitude = item.latitude;
longitude = item.longitude;
geocoder = new google.maps.Geocoder();
latlng = new google.maps.LatLng(latitude, longitude);

function codeLatLng() {
var input = document.getElementById('latlng').value;
var latlngStr = input.split(',', 2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({
'latLng': latlng
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if(results[0]){

var town = extractFromAdress(results[0].address_components, "locality","administrative_area_level_2");
if (town == cityName) {
inside += 1;
} else {
outside += 1;
}
}
}
});
}
});
inLosAngeles = inside;
notLosAngeles = outside;
$('#results').append('inLosAngeles' + inLosAngeles + '' + 'notLosAngeles' + notLosAngeles);
});
});
</script>
</head>

<body>

<div id = "results"></div>
</body>

</html>

关于javascript - 从反向地理编码中提取城市,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19153632/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com