我正在使用google geolocation的getCurrentPosition()函数来获取用户的当前位置。
它在firefox中对我很好,但不适用于chrome。
我的代码如下::
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<p id="demo">Click the button to get your position.</p>
<button onclick="getLocation()">Try It</button>
<div id="mapholder"></div>
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showtemp);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
var latlon = position.coords.latitude + "," + position.coords.longitude;
var img_url = "http://maps.googleapis.com/maps/api/staticmap?center="
+latlon+"&key=AIzaSyDOgvRydLLNrztjgagobYS_sROK1u3r4M4&zoom=14&size=400x300&sensor=false";
document.getElementById("mapholder").innerHTML = "<img src='"+img_url+"'>";
}
function showtemp(temp) {
alert("test");
}
function showError(error) {
$.get("http://ipinfo.io", function (response) {
var array = (response.loc).split(',');
console.log(array[0]);
var latlon = array[0] + "," + array[1];
var img_url = "http://maps.googleapis.com/maps/api/staticmap?center="
+latlon+"&zoom=14&size=400x300&sensor=false";
document.getElementById("mapholder").innerHTML = "<img src='"+img_url+"'>";
}, "jsonp");
}
</script>
</body>
</html>
请帮我解决这个问题。
它给了我错误::“getCurrentPosition()和watchPosition()在不安全的起源上被弃用,并且将来会删除支持。您应该考虑将应用程序切换到安全的来源,例如HTTPS。” p>
提前致谢
答案 0 :(得分:0)
getcurrentposition()被删除,没有替换它。阅读此答案: - getcurrentposition-and-watchposition-are-deprecated-on-insecure-origins
点击此谷歌更新的api示例链接它的工作示例。 :https://developers.google.com/maps/documentation/javascript/examples/map-geolocation。
将鼠标悬停在代码块的右上角以复制代码或在JSFiddle中打开它。
使用此功能:
// Note: This example requires that you consent to location sharing when
// prompted by your browser. If you see the error "The Geolocation service
// failed.", it means you probably did not give permission for the browser to
// locate you.
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 6
});
var infoWindow = new google.maps.InfoWindow({map: map});
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('Location found.');
map.setCenter(pos);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
}
</script>
答案 1 :(得分:0)
地理位置本身并未弃用,但仅限于通过HTTPS投放的网站。
警告本身读取&#34; getCurrentPosition()和watchPosition()在不安全的起源&#34;上已弃用,它会缩减为通过HTTP而非HTTPS提供的页面。
请注意,您的代码在最新的Chrome中运行良好here。
获取SSL的最简单方法可能是使用Github pages来托管您的内容或使用surge内容。
答案 2 :(得分:0)
您可以使用https://ipinfo.io API(我的服务)作为require(rootSolve)
require(msm)
phi <- function(z) {
dnorm(z)
}
Phi <- function(z) {
pnorm(z)
}
Mean <- function(mu, sigma, a, b) {
alfa <- (a - mu) / sigma
beta <- (b - mu) / sigma
Z <- Phi(beta) - Phi(alfa)
mu + sigma*(phi(alfa) - phi(beta))/Z
}
f <- function(mu, mean, sigma, a, b) {
mean - Mean(mu, sigma, a, b)
}
a <- 50000.0
b <- 250000.0
mean <- 70000.0
sigma <- 24000.0
# find mu for desired mean
q <- uniroot(f, c(a, b), mean, sigma, a, b)
mu <- q$root
print(sprintf("Found mu = %f for the desired mean %f and sigma %f", mu, mean, sigma))
# sampling test
set.seed(32345)
N = 100000
r <- rtnorm(N, mean=mu, sd=sigma, lower=a, upper=b)
print(sprintf("Sampled %d truncated gaussians and got observed mean = %f", N, mean(r)))
的替代方案。它可以免费获得高达1,000 req / day(有或没有SSL支持)。它为您提供坐标,名称等,适用于非SSL站点,并且不会提示用户进行许可。这是一个例子:
getCurrentLocation()
这是一个使用API响应构建coords对象的示例,该响应与您从curl ipinfo.io
{
"ip": "172.56.39.47",
"hostname": "No Hostname",
"city": "Oakland",
"region": "California",
"country": "US",
"loc": "37.7350,-122.2088",
"org": "AS21928 T-Mobile USA, Inc.",
"postal": "94621"
}
获得的内容相匹配:
getCurrentPosition()
这是一个详细的示例,展示了如何将其用作$.getJSON('https://ipinfo.io/geo', function(response) {
var loc = response.loc.split(',');
var coords = {
latitude: loc[0],
longitude: loc[1]
};
});
的后备:
getCurrentPosition()
有关详细信息,请参阅http://ipinfo.io/developers/replacing-navigator-geolocation-getcurrentposition。