beego+GeoLite2免费数据库获取ip地址经纬度等定位归属信息
GeoLite2数据库是免费的IP地理定位数据库
支持离线数据库下载,还免费,当然也有付费的比免费的更准确详情等等,
直接从用户的IP地址中获取用户的经度和纬度等等更详细的信息
官方网站文档:
https://dev.maxmind.com/geoip/geolite2-free-geolocation-data?lang=en
GeoLite2离线数据库文件(GeoLite2-City.mmdb.gz)下载地址:
https://www.zongscan.com/file/49.html
依赖包
"github.com/oschwald/geoip2-golang"
路由:(\routers\router.go)
//IP地址定位
beego.Router("/iptolocation", &controllers.IndexController{}, "get:IpToLocation;post:IpToLocation")
ip地址定位测试
控制器
package controllers
import (
"fmt"
"gblog/models"
"net"
"github.com/oschwald/geoip2-golang"
"github.com/astaxie/beego/orm"
)
type IndexController struct {
BaseController
}
//ip定位
func (c *IndexController) IpToLocation() {
c.Data["title"] = " GeoLite2 IP地址定位"
c.Data["keywords"] = "GeoLite2,IP地址定位"
c.Data["description"] = "GeoLite2 IP地址定位"
db, err := geoip2.Open("static/GeoLite2-City.mmdb")
if err != nil {
fmt.Println(err)
}
defer db.Close()
if c.Ctx.Request.Method == "POST" {
ipAddress := c.GetString("ipaddress")
if ipAddress != "" {
ip := net.ParseIP(ipAddress)
record, err := db.City(ip)
if err != nil {
fmt.Println(err)
}
c.SuccessJson(record)
}
}
c.TplName = "iptolocation.html"
}
视图:
{{template "public/header.html" .}}
<div class="row clearfix">
<div class="col-md-12 column">
<div class="row clearfix">
<div class="col-md-4 column"></div>
<div class="col-md-4 column">
<h3 style="padding: 5rem;">IP地址定位信息查询</h3>
<form id="myForm" action="/iptolocation" method="post" role="form">
<div>
<label for="inputEmail3" class="col-sm-2 control-label">IP</label>
<div>
<input type="name" name="ipaddress" id="ipaddress" placeholder="请输入ip地址" />
</div>
</div>
<div id="ipinfo" class="alert alert-success alert-dismissable">
<button type="button" data-dismiss="alert" aria-hidden="true">×</button>
<pre></pre>
</div>
<div>
<div class="col-sm-offset-2 col-sm-10"><button type="submit" class="btn btn-default">查询ip地址信息</button></div>
</div>
</form>
</div>
<div class="col-md-4 column"></div>
</div>
</div>
</div>
<script>
$(document).ready(function() {
// bind form using ajaxForm
$('#myForm').ajaxForm({
dataType: 'json',
// success: processJson
success: function(data){ console.log(data);
if (data.Code == 200) {
$("#ipinfo pre").html(syntaxHighlight(data));
}
}
});
});
//html json格式化显示
function syntaxHighlight(json) {
if (typeof json != 'string') {
//JSON化的对象,2是spacing
json = JSON.stringify(json, undefined, 2);
}
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function(match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
}
</script>
{{template "public/footer.html" .}}
返回全部json数据:
{
"Code": 200,
"Msg": "success",
"Data": {
"City": {
"GeoNameID": 1809858,
"Names": {
"de": "Guangzhou",
"en": "Guangzhou",
"es": "Cantón",
"fr": "Canton",
"ja": "広州",
"pt-BR": "Cantão",
"ru": "Гуанчжоу",
"zh-CN": "广州市"
}
},
"Continent": {
"Code": "AS",
"GeoNameID": 6255147,
"Names": {
"de": "Asien",
"en": "Asia",
"es": "Asia",
"fr": "Asie",
"ja": "アジア",
"pt-BR": "Ásia",
"ru": "Азия",
"zh-CN": "亚洲"
}
},
"Country": {
"GeoNameID": 1814991,
"IsInEuropeanUnion": false,
"IsoCode": "CN",
"Names": {
"de": "China",
"en": "China",
"es": "China",
"fr": "Chine",
"ja": "中国",
"pt-BR": "China",
"ru": "Китай",
"zh-CN": "中国"
}
},
"Location": {
"AccuracyRadius": 500,
"Latitude": 23.1181,
"Longitude": 113.2539,
"MetroCode": 0,
"TimeZone": "Asia/Shanghai"
},
"Postal": {
"Code": ""
},
"RegisteredCountry": {
"GeoNameID": 1814991,
"IsInEuropeanUnion": false,
"IsoCode": "CN",
"Names": {
"de": "China",
"en": "China",
"es": "China",
"fr": "Chine",
"ja": "中国",
"pt-BR": "China",
"ru": "Китай",
"zh-CN": "中国"
}
},
"RepresentedCountry": {
"GeoNameID": 0,
"IsInEuropeanUnion": false,
"IsoCode": "",
"Names": null,
"Type": ""
},
"Subdivisions": [
{
"GeoNameID": 1809935,
"IsoCode": "GD",
"Names": {
"en": "Guangdong",
"fr": "Province de Guangdong",
"zh-CN": "广东"
}
}
],
"Traits": {
"IsAnonymousProxy": false,
"IsSatelliteProvider": false
}
}
}
看看效果:
完
相关文章