首页 > 解决方案 > 如何使用 IP 地理定位记录位置?

问题描述

我有一个使用 Geolocation DB 脚本的脚本,该脚本根据用户的 IP 跟踪用户的位置。我是新手,正在寻找一种方法来记录被跟踪的位置,以便我可以看到它们。

这是脚本/HTML。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>

    <div>State: <span id="state"></span></div>
    <div>City: <span id="city"></span></div>
    <div>Latitude: <span id="latitude"></span></div>
    <div>Longitude: <span id="longitude"></span></div>
    <div>IP: <span id="ip"></span></div>
    <script>
      $.getJSON('https://geolocation-db.com/json/')
         .done (function(location) {
            $('#country').html(location.country_name);
            $('#state').html(location.state);
            $('#city').html(location.city);
            $('#latitude').html(location.latitude);
            $('#longitude').html(location.longitude);
            $('#ip').html(location.IPv4);
         });
    </script>

因此它将在用户的屏幕上显示用户的位置。我需要一种方法来查看它。我该怎么办?谢谢!

标签: javascripthtmlcssgeolocationip-address

解决方案


看看这个。我已经纠正了这一点。

$(document).ready(function(){
    $.getJSON('https://geolocation-db.com/json/')
       .done (function(location) {
          $('#country').html(location.country_name);
          $('#state').html(location.state);
          $('#city').html(location.city);
          $('#latitude').html(location.latitude);
          $('#longitude').html(location.longitude);
          $('#ip').html(location.IPv4);
       });
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>State: <span id="country"></span></div>
<div>State: <span id="state"></span></div>
<div>City: <span id="city"></span></div>
<div>Latitude: <span id="latitude"></span></div>
<div>Longitude: <span id="longitude"></span></div>
<div>IP: <span id="ip"></span></div>


推荐阅读