我遇到了创建Google地图启动链接的问题,该链接必须将“当前位置”字符串作为源地址。
问题在于需要生成此链接并与某人分享。在这种情况下,需要知道朋友智能手机的区域设置是什么,以便生成正确的链接。
例如,以下链接仅适用于具有英语或英语区域设置的设备: http://maps.google.com/maps?saddr=Current%20Location&daddr=32.026685,34.742092
是否存在创建此类链接的通用方法,以便它可以在所有设备上运行,无论其语言环境如何。
答案 0 :(得分:1)
问题在于Google地图启动链接中的saddr
参数。它必须是三个选项之一:
前两个没问题,但在大多数情况下,第三个被视为地址字符串而不是获取当前位置的命令。如果它与启动链接的电话位于相同的区域设置,则它可以正常工作。例如,“Lieu actuel”是Franch中的“当前位置”,将在区域设置为法语的设备上工作,但不适用于具有任何其他区域设置的设备(例如英语) 如果你知道接收者的本地,这个事实可能不是问题,但在大多数情况下你不是。
我的解决方案是使用帮助HTML页面接收目标地址/点作为GET参数,并使用适当的区域设置中的用户位置点或“当前位置”字符串生成Google启动链接作为源地址/点
我正在使用浏览器的地理位置api来检索“当前位置”字符串的位置和静态翻译数组。
这是HTML帮助页面的简单视觉结构:
这是完整的HTML页面代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html>
<head>
</style>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-8-i">
<title>My Title!</title>
<script type="text/javascript">
function getQueryVariable(variable) {
variable = variable.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + variable + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(document.URL);
if (results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
var langArr = new Array();
langArr["nl"] = "Huidige locatie";
langArr["en"] = "Current Location";
langArr["fr"] = "Lieu actuel";
langArr["de"] = "Aktueller Ort";
langArr["it"] = "Posizione attuale";
langArr["ja"] = "???";
langArr["es"] = "Ubicacion actual";
langArr["ar"] = "?????? ??????";
langArr["ca"] = "Ubicacio actual";
langArr["cs"] = "Soucasna poloha";
langArr["da"] = "Aktuel lokalitet";
langArr["el"] = "???????? ?????????";
langArr["en-gb"] = "Current Location";
langArr["en-us"] = "Current Location";
langArr["fi"] = "Nykyinen sijainti";
langArr["he"] = "????? ?????";
langArr["he-il"] = "????? ?????";
langArr["hr"] = "Trenutna lokacija";
langArr["hu"] = "Jelenlegi helyszin"
langArr["id"] = "Lokasi Sekarang";
langArr["ko"] = "?? ??";
langArr["ms"] = "Lokasi Semasa";
langArr["no"] = "Nav?rende plassering";
langArr["pl"] = "Biezace polozenie";
langArr["pt"] = "Localizacao Atual";
langArr["pt-pt"] = "Localizacao actual";
langArr["ro"] = "Loc actual";
langArr["ru"] = "Òåêóùåå ðàçìåùåíèå";
langArr["ru-ru"] = "Òåêóùåå ðàçìåùåíèå";
langArr["sk"] = "Aktualna poloha";
langArr["sv"] = "Nuvarande plats";
langArr["th"] = "???????????????";
langArr["tr"] = "Su Anki Yer";
langArr["uk"] = "Ïîòî÷íå ì³ñöå";
langArr["vi"] = "V? tri Hi?n t?i";
langArr["zh-cn"] = "????";
langArr["zh-tw"] = "????";
var coordinate = getQueryVariable("ll");
var language = window.navigator.userLanguage || window.navigator.language;
language = language.toLowerCase();
if (navigator.geolocation != undefined)
{
if (coordinate != "")
navigator.geolocation.getCurrentPosition(successCallback,
errorCallback,
{maximumAge:000000});
}
else
{
errorCallback("");
}
function successCallback(position) {
if ( (navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPad') != -1))
window.location = "comgooglemaps://?saddr=" + position.coords.latitude + "," + position.coords.longitude + "&daddr=" + coordinate;
else
window.location = "http://maps.google.com/maps?saddr=" + position.coords.latitude + "," + position.coords.longitude + "&daddr=" + coordinate;
}
function errorCallback(error) {
alert("Sorry cannot determine your current location. Please select \"Current Location\" as point A in google maps");
window.location = "http://maps.google.com/maps?saddr=" + langArr[language].replace(" ","+") + "&daddr=" + coordinate;
}
</script>
</head>
<body>
<div STYLE="position:absolute; TOP:460px; LEFT:80px; WIDTH:370px; HEIGHT:50px" >
Please wait.<br/>Detecting your location...<br/>If nothing happens for long time please use the following link:
<h3><a href="javascript:errorCallback('');">Manually launch Google Maps</a></h3>
</div>
</body>
</html>