0

我有两个 IP 地理定位解决方案,但代码似乎不适用于两种解决方案。

解决方案1:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
    $.get("http://ipinfo.io", function (response) {
        $("#ip").html("IP: " + response.ip);
        $("#address").html("Location: " + response.city + ", " + response.region);
        $("#details").html(JSON.stringify(response, null, 4));
    }, "jsonp");
</script>
</head>
<body>
<h3>Client side IP geolocation using <a href="http://ipinfo.io">ipinfo.io</a></h3>

<hr/>
<div id="ip"></div>
<div id="address"></div>
<hr/>Full response: <pre id="details"></pre>
</body>
</html>

这个在屏幕上显示“完全响应”,所以代码的结果(位置)应该在那里,但它是空白的。

解决方案2:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="gears_init.js"></script>
<script type="text/javascript">
var geo = google.gears.factory.create('beta.geolocation');

function updatePosition(position) {
  alert('Current lat/lon is: ' + position.latitude + ',' + position.longitude);
}

function handleError(positionError) {
  alert('Attempt to get location failed: ' + positionError.message);
}

geo.getCurrentPosition(updatePosition, handleError);
</script>
</head>
<body>

</body>
</html>

第二个解决方案来自一个问题here,但是那个人有这个代码工作,但想计算两个位置之间的距离,但我似乎无法获得基本功能的工作,它显示一个空白页,没有警报或任何东西.

谁能帮我解决这些问题。解决其中一个就足够了:)

4

1 回答 1

1

第一个解决方案不起作用,因为您使用了 jQuery 的 $ 对象,但没有先导入 jQuery。这是有效的代码:

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript">
    $.get("http://ipinfo.io", function (response) {
        $("#ip").html("IP: " + response.ip);
        $("#address").html("Location: " + response.city + ", " + response.region);
        $("#details").html(JSON.stringify(response, null, 4));
    }, "jsonp");
</script>
</head>
<body>
<h3>Client side IP geolocation using <a href="http://ipinfo.io">ipinfo.io</a></h3>

<hr/>
<div id="ip"></div>
<div id="address"></div>
<hr/>Full response: <pre id="details"></pre>
</body>
</html>

至于第二种解决方案,它似乎正在使用 Google Gears API,它似乎不再可用——所以你不会有任何运气。

于 2013-10-11T16:44:29.590 回答