jquery 获取 IP地址与地理位置
有勇气的牛排
1235
前端
2023-05-18 22:22:16
知止自律打卡-微信小程序APP
一款微信自律打卡小程序,支持自定义打卡项目、习惯记录、项目日记、专注计时与数据统计。自律打卡、学习打卡、读书打卡、健身打卡、运动打卡、减肥打卡、戒色打卡、养生打卡、戒烟、戒酒、戒熬夜等多种场景。万能打卡模板自由创建。可自定义每日打卡目标,记录习惯连续天数,可视化追踪成长进度。
了解产品详情 →
微信搜索「知止自律打卡」
1. 接口
搜狐IP地址查询接口(默认GBK):http://pv.sohu.com/cityjson
搜狐IP地址查询接口(可设置编码):http://pv.sohu.com/cityjson?ie=utf-8
获取城市位置:http://api.map.baidu.com/location/ip?ak=ia6HfFL660Bvh43exmH9LrI6
2 获取地理位置 具体到市
$(function () {
$.ajax({
url: 'http://api.map.baidu.com/location/ip?ak=ia6HfFL660Bvh43exmH9LrI6',
type: 'POST',
dataType: 'jsonp',
success: function (res) {
console.log(res)
console.log(res.address)
console.log(res.content.address)
console.log(res.content.address_detail.adcode)
console.log(res.content.address_detail.city)
console.log(res.content.address_detail.city_code)
console.log(res.content.address_detail.district)
console.log(res.content.address_detail.province)
console.log(res.content.address_detail.street)
console.log(res.content.address_detail.street_number)
}
});
});
<script src="jquery.min.js"></script>
<div>
<input id="province" type="text">
</div>
<button id="submit">提交</button>
<script src="position.js"></script>
3 获取IP地址
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
$("span").html(returnCitySN["cip"]);
<span></span>

<h2><a id="1__0"></a>1. 接口</h2>
<p>搜狐IP地址查询接口(默认GBK):<a href="http://pv.sohu.com/cityjson" target="_blank">http://pv.sohu.com/cityjson</a><br />
搜狐IP地址查询接口(可设置编码):<a href="http://pv.sohu.com/cityjson?ie=utf-8" target="_blank">http://pv.sohu.com/cityjson?ie=utf-8</a><br />
获取城市位置:<a href="http://api.map.baidu.com/location/ip?ak=ia6HfFL660Bvh43exmH9LrI6%27" target="_blank">http://api.map.baidu.com/location/ip?ak=ia6HfFL660Bvh43exmH9LrI6</a></p>
<h2><a id="2___5"></a>2 获取地理位置 具体到市</h2>
<pre><div class="hljs"><code class="lang-javascript">$(<span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) {
$.<span class="hljs-title function_">ajax</span>({
<span class="hljs-attr">url</span>: <span class="hljs-string">'http://api.map.baidu.com/location/ip?ak=ia6HfFL660Bvh43exmH9LrI6'</span>,
<span class="hljs-attr">type</span>: <span class="hljs-string">'POST'</span>,
<span class="hljs-attr">dataType</span>: <span class="hljs-string">'jsonp'</span>,
<span class="hljs-attr">success</span>: <span class="hljs-keyword">function</span> (<span class="hljs-params">res</span>) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(res)
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(res.<span class="hljs-property">address</span>)
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(res.<span class="hljs-property">content</span>.<span class="hljs-property">address</span>)
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(res.<span class="hljs-property">content</span>.<span class="hljs-property">address_detail</span>.<span class="hljs-property">adcode</span>)
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(res.<span class="hljs-property">content</span>.<span class="hljs-property">address_detail</span>.<span class="hljs-property">city</span>)
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(res.<span class="hljs-property">content</span>.<span class="hljs-property">address_detail</span>.<span class="hljs-property">city_code</span>)
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(res.<span class="hljs-property">content</span>.<span class="hljs-property">address_detail</span>.<span class="hljs-property">district</span>)
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(res.<span class="hljs-property">content</span>.<span class="hljs-property">address_detail</span>.<span class="hljs-property">province</span>)
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(res.<span class="hljs-property">content</span>.<span class="hljs-property">address_detail</span>.<span class="hljs-property">street</span>)
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(res.<span class="hljs-property">content</span>.<span class="hljs-property">address_detail</span>.<span class="hljs-property">street_number</span>)
}
});
});
</code></div></pre>
<pre><div class="hljs"><code class="lang-html"> <span class="hljs-tag"><<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"jquery.min.js"</span>></span><span class="hljs-tag"></<span class="hljs-name">script</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span>></span>
<span class="hljs-tag"><<span class="hljs-name">input</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"province"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span>></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"><<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"submit"</span>></span>提交<span class="hljs-tag"></<span class="hljs-name">button</span>></span>
<span class="hljs-tag"><<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"position.js"</span>></span><span class="hljs-tag"></<span class="hljs-name">script</span>></span>
</code></div></pre>
<h2><a id="3_IP_39"></a>3 获取IP地址</h2>
<pre><div class="hljs"><code class="lang-javascript"><script src=<span class="hljs-string">"http://pv.sohu.com/cityjson?ie=utf-8"</span>></script>
</code></div></pre>
<pre><div class="hljs"><code class="lang-javascript">$(<span class="hljs-string">"span"</span>).<span class="hljs-title function_">html</span>(returnCitySN[<span class="hljs-string">"cip"</span>]);
</code></div></pre>
<pre><div class="hljs"><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">span</span>></span><span class="hljs-tag"></<span class="hljs-name">span</span>></span>
</code></div></pre>
<p><img src="https://www.couragesteak.com/tcos/article/92e9784faeedb0cb8952486d2d40aa0b.png" alt="image.png" /></p>
评论区