[英]geocode reverse
我正在使用以下代碼來調用地圖並顯示。 我想添加該功能以進行反向地址解析並打印出相應的街道地址。 我似乎無法將長/緯度坐標傳遞給函數。 如何將經緯度坐標轉換為街道地址?
function getMyLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
displayLocation, displayError, {
enableHighAccuracy: true,
timeout: 9000
});
var watchButton = document.getElementById("watch");
watchButton.onclick = watchLocation;
var clearWatchButton = document.getElementById("clearWatch");
clearWatchButton.onclick = clearWatch;
}
else {
alert("Oops, no geolocation support");
}
}
function displayLocation(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var div = document.getElementById("location");
div.innerHTML = "You are at Latitude: " + latitude + ", Longitude: " + longitude;
div.innerHTML += " (with " + position.coords.accuracy + " meters accuracy)";
var km = computeDistance(position.coords, ourCoords);
var distance = document.getElementById("distance");
distance.innerHTML = "You are " + km + " km from the WickedlySmart HQ";
if (map == null) {
showMap(position.coords);
prevCoords = position.coords;
}
else {
var meters = computeDistance(position.coords, prevCoords) * 1000;
if (meters > 20) {
scrollMapToPosition(position.coords);
prevCoords = position.coords;
}
}
}
除了手表按鈕附近的區域外,其余的一切在Chrome瀏覽器中對我來說都很正常。
您不是要通過file:// URL來執行此操作,對嗎?
抱歉。 誤解了這個問題。 這是幾周前我為一個項目編寫的一些代碼。 它只是拉郵政編碼,但是您可以從中獲取地址。 它是為jQuery而編寫的。 另外,我正在動態加載Google Maps API。 如果需要,您可以直接加載它。
$(document).ready(function() {
if (navigator.geolocation) {
var script = document.createElement("script");
script.type = 'text/javascript';
script.src = 'http://www.google.com/jsapi?callback=loadMapsAPI';
document.getElementsByTagName('head')[0].appendChild(script);
} else {
console.log('NOTE: Browser does not support geolocation.');
}
});
function loadMapsAPI() {
google.load('maps', '3', {
'other_params': 'sensor=false',
'callback' : mapLoaded
}
);
}
function mapLoaded() {
$('#geolocateButton').css('display','block');
}
function findMyLocation() {
navigator.geolocation.getCurrentPosition(
function(position) {
var latLng = new google.maps.LatLng(
position.coords.latitude,
position.coords.longitude);
var coder = new google.maps.Geocoder();
coder.geocode({ 'latLng': latLng }, fillZipcode);
},
function() {
alert("Could not determine your position.");
}
);
}
function fillZipcode(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
var parts = results[1].address_components;
for(var i = 0; i < parts.length; i++) {
if(parts[i].types[0] == 'postal_code') {
alert('Your Zip Code is: ' + parts[i].short_name);
return;
}
}
} else {
alert("Could not figure out your zip code.");
}
} else {
alert("Geocoder failed due to: " + status);
}
}
如果您想使用其他庫,可以在此處查看geous及其jquery插件的最終地理編碼/位置示例。
該示例使用setLocation
方法自動填充表單,但您可以輕松地打印位置:
// location geocoded -- print address
var onGeocodeSuccess = function (location) {
console.log(location.toAddress());
}
// location retrieved -- geocode it.
var onUserLocationSuccess = function (location) {
geous.geocode (location, {
reverse: true,
success: onGeocodeSuccess
});
}
// Try and get user location
geous.getUserLocation({
success: onUserLocationSuccess
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.