如果地理位置被拒绝,我需要JavaScript才能显示手动输入.
我尝试过的:
Modernizr.geolocation
navigator.geolocation
Run Code Online (Sandbox Code Playgroud)
两者均未描述用户之前是否拒绝访问地理位置.
所以我有这个javascript代码.在safari和chrome中,如果用户拒绝共享位置,它将失败,因为它应该; 但是,在Firefox中,它没有.
任何帮助赞赏.
function initGeolocation()
{
if( navigator.geolocation )
{
// Call getCurrentPosition with success and failure callbacks
navigator.geolocation.getCurrentPosition( success, fail );
}
else
{
alert("Sorry, your browser does not support geolocation services.");
}
}
var map;
function success(position)
{
var longIDText = document.getElementById('longID');
var latIDText = document.getElementById('latID');
longIDText.value = position.coords.longitude;
latIDText.value = position.coords.latitude;
document.getElementById('coordSubmitID').click();
}
function fail(error)
{
alert("FAAAAAAAAAAIIIIIIIIIL")
var zip_code ;
while (true){
// Could not obtain location
zip_code = prompt("Please enter your current address or zip code",""); …
Run Code Online (Sandbox Code Playgroud) 我正在尝试访问Android WebView中提供的HTML Geolocation API(使用SDK版本24).
主要问题是JavaScript中的调用navigator.geolocation.getCurrentPosition()
永远不会返回(既没有错误,也没有位置数据),而在应用程序端,我检查权限并使用android.webkit.GeolocationPermissions.Callback
类正确地将它们传递给WebView .
更新:这里只是澄清一下,"永不返回"我的意思是没有任何太多提供的回调navigator.geolocation.getCurrentPosition(success, error)
被调用.
在我为测试它而构建的示例应用程序(只有一个托管WebView的小活动)我在清单中声明权限并在App start上正确请求它们.我看到提示,可以授予或拒绝位置信息的许可.
表现:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Run Code Online (Sandbox Code Playgroud)
主要形式的代码:
public boolean checkFineLocationPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION) && !mIsPermissionDialogShown) {
showPermissionDialog(R.string.dialog_permission_location);
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSION_ACCESS_FINE_LOCATION);
}
return false;
} else {
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
我可以在运行时使用期间检查权限Context.checkSelfPermission()
,我看到相应的权限被授予我的应用程序.
然后我尝试在WebView
控件中打开一个网页.我在设置中启用了所有必需的选项:
mWebSettings.setJavaScriptEnabled(true);
mWebSettings.setAppCacheEnabled(true);
mWebSettings.setDatabaseEnabled(true);
mWebSettings.setDomStorageEnabled(true);
mWebSettings.setGeolocationEnabled(true);
mWebSettings.setJavaScriptCanOpenWindowsAutomatically(true);
mWebSettings.setSupportZoom(true);
Run Code Online (Sandbox Code Playgroud)
我使用以下 …
我正在构建一个使用Geolocation API的应用程序.我似乎无法在Firefox 10上使用一段非常简单的代码.以下是代码:
window.onload = function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
alert('it works');
}, function(error) {
alert('Error occurred. Error code: ' + error.code);
});
}else{
alert('no geolocation support');
}
};
Run Code Online (Sandbox Code Playgroud)
因此,例如,在Chrome中,在运行页面后,系统会询问我是否要共享我的位置,点击"是"后会提醒我"它有效".现在在Firefox 10中,它会让我分享我的位置,点击分享后它什么也没做......我一直试图让回调运行任何类型的代码,但没有运气.这是Firefox的错误还是我做错了什么?我在这里有一个代码测试示例:http://dev-hub.com/geolocation.html.
编辑---我的操作系统是Windows 7 64位
我有一个按钮,我已经将onclick挂钩到一个调用来检索用户位置,然后使用该位置再次调用以检索附近位置的列表.由于某种原因,在第二次单击按钮时会调用地理定位成功方法两次.
所以我加载页面,单击按钮,允许使用我的位置,它将发出一个ajax请求来获取附近的位置.这很好用.
我再次单击该按钮,允许使用我的位置(再次),它将发出一个ajax请求获取位置,等待~2秒,然后使用相同的坐标发出另一个请求,而不允许我.所以成功方法必须被调用两次,但我不确定为什么.
$("#FindLocation").click(function () {
myScript.findNearbyLocations(displayData, displayError);
});
Run Code Online (Sandbox Code Playgroud)
这个click函数没有被调用两次,我已经注释掉了displayData和displayError中的所有代码.
findNearbyLocations: function (onSuccess, onFailure) {
if (navigator.geolocation) {
browserSupportFlag = true;
navigator.geolocation.getCurrentPosition(function (position) {
alert('This is getting called twice except on the initial call.');
myScript.findStores(position.coords.latitude, position.coords.longitude, onSuccess, onFailure);
}, function () {
onFailure(true);
});
}
}
Run Code Online (Sandbox Code Playgroud)
谁知道我哪里出错了?
编辑:我不认为标记是问题.我只使用基本网页进行测试.目前没有造型或其他元素.
<form id="form1" runat="server">
<div>
<MyControls:Search ID="Search" runat="server" />
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
和用户控件(以及所有必要的javascript包括)
<script type="text/javascript">
$(document).ready(function () {
$("#FindLocation").click(function () {
myScript.findNearbyLocations(displayData, displayError);
});
});
</script>
<input type="button" id="FindLocation" value="Find Location" />
<div id="results"> …
Run Code Online (Sandbox Code Playgroud) 大家好,我有以下代码:
var map;
var infowindow;
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(initialize);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function initialize(position) {
var pyrmont = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: pyrmont,
zoom: 15
});
var request = {
location: pyrmont,
radius: 500,
types: ['restaurant']
};
...
Run Code Online (Sandbox Code Playgroud)
基本上,如果我设置 Long/Lat 坐标,我可以让地图工作得很好,但我希望通过获取用户位置来传递这些坐标。使用上面的代码,地图显示在我的手机上,但出现以下错误:
TypeError: 'undefined' is not an object (evaluating 'position.coords.latitude')
Run Code Online (Sandbox Code Playgroud)
但在桌面上我没有得到地图和以下错误:
Uncaught TypeError: Cannot read property 'latitude' of undefined
Run Code Online (Sandbox Code Playgroud)
任何帮助都会很棒我是 Javascript 和这些 API 的新手
我创建了一个应用程序,并且作为数据收集的一部分,我想在用户使用php访问应用程序和网站时捕获用户的当前位置.
理想情况下,我希望尽可能简化这一点.目前,我有以下脚本,但它有一个默认地址:
$fullurl = "http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true";
echo $json;
$string .= file_get_contents($fullurl); // get json content
$json_a = json_decode($string, true); //json decoder
echo $json_a['results'][0]['geometry']['location']['lat']; // get lat for json
echo $json_a['results'][0]['geometry']['location']['lng']; // get ing for json
Run Code Online (Sandbox Code Playgroud)
我希望用户的当前位置取代1600 Amphitheatre Parkway,Mountain View,CA.
非常感谢任何帮助.
javascript ×7
geolocation ×6
firefox ×2
google-maps ×2
html5 ×2
jquery ×2
android ×1
callback ×1
google-api ×1
html ×1
php ×1