IP定位,騰訊定位周邊數(shù)據(jù)分享
這些天有空,把IP查詢及IP模糊定位的工具更新了一遍,開始用高德,寫完后發(fā)現(xiàn)查詢幾次要“阿里式滑動”驗證,故放棄了。
針對http://suancuo.cn/ip/ 工具,我真心怕你們“程序員”去爬蟲輸出結果,請看到這些分享,自己實現(xiàn),有難點可以咨詢我,不要采用爬蟲的方式去實現(xiàn),因為本站工具頗多,基本上好多工具都被程序員爬蟲或者模擬正常使用者大量不正常使用,而且好多“素質低”完全不管本站死活,造成本站間接性很卡。在此懇求就算用爬蟲去爬取別人的東西來實現(xiàn)某一個業(yè)務點,那也請節(jié)制一點,做好優(yōu)化,如緩存等等,還是那句話,不要把免費的資源不當資源。
騰訊IP定位申請
在騰訊地圖開放平臺“https://lbs.qq.com/”用你的QQ號或者微信號登陸,填寫個人信息,手機號及郵箱認證,就有幾萬的額度,有企業(yè)資質審核通過就會有幾十萬的免費額度。
創(chuàng)建一個KEY:https://lbs.qq.com/console/mykey.html?console=mykey
配額查看管理:https://lbs.qq.com/console/myquota.html?console=myquota
點擊提升配額需要填寫企業(yè)信息,有30萬的免費額度,一般的都夠用了,填寫完畢后會3個工作日審核
騰訊IP定位Java代碼實現(xiàn)
Http.Response response = Http.create("http://apis.map.qq.com/ws/location/v1/ip?key=你的key&ip=%s", ip)
.get()
.timeout(2)
.send().getResponse();
if(response!=null && response.getStatus() == 200){
String result = response.getResult();
JSONObject json = JSONObject.fromObject(result);
//判斷是否無法精準定位
if(json.getInt("status") == 0 ){//可以定位
JSONObject location = json.getJSONObject("result").getJSONObject("location");
//不是默認定位值
String lng = location.getString("lng");
String lat = location.getString("lat");
//騰訊定位要是定位不到,就是默認北京的這個"116.407526"和"39.90403"
if(!("116.407526".equals(lng) && "39.90403".equals(lat))){
try {
//開始精準定位
Http.Response jresponse = Http.create("https://apis.map.qq.com/ws/geocoder/v1/?location=%s,%s&get_poi=1&key=你的key", lat,lng)
.head("Host","map.qq.com")
.head("Accept","*/*")
.head("Accept-Encoding","gzip, deflate, br")
.head("Accept-Language","zh-CN,zh;q=0.9,en;q=0.8")
.head("Cache-Control","no-cache")
.head("Referer","https://map.qq.com/")
.head("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36")
.head("X-Requested-With","XMLHttpRequest")
.get()
.timeout(2)
.send().getResponse();
if(jresponse!=null && jresponse.getStatus() == 200 && StringUtils.isNotBlank(jresponse.getResult())){
JSONObject jjson = JSONObject.fromObject(jresponse.getResult());
//成功
if(Constant.S_ZERO.equals(jjson.optString("status"))){
JSONObject resultJSON = jjson.optJSONObject("result");
if(null != resultJSON && resultJSON.size() > 0){
bo.setAddress(resultJSON.optString("address"));
JSONObject ad_info = resultJSON.optJSONObject("ad_info");
bo.setDistrictadcode(ad_info.optString("adcode"));
JSONArray poiList = resultJSON.optJSONArray("pois");
//轉換
if(null != poiList && poiList.size() > 0){
List<IPBo.PoiList> poiLists = new ArrayList<>();
poiList.forEach(e-> poiLists.add(new IPBo.PoiList(e)));
bo.setPoiLists(poiLists);
}
//地區(qū)詳細詳細
JSONObject address_component = resultJSON.optJSONObject("address_component");
// "nation":"中國",
// "province":"湖南省",
// "city":"長沙市",
// "district":"岳麓區(qū)",
// "street":"岳麓大道",
// "street_number":"岳麓大道218號"
bo.setNation(address_component.optString("nation"));
bo.setProvince(address_component.optString("province"));
//市
bo.setCity(address_component.optString("city"));
//區(qū),可能為空字串
bo.setDistrict(address_component.optString("district"));
//街道
bo.setStreet(address_component.optString("street"));
//門牌,可能為空字串
bo.setStreetNumber(address_component.optString("street_number"));
}
}
}
//開啟精準定位
bo.setLocation(Boolean.TRUE);
} catch (Exception e) {
log.error("IP 查詢精準定位請求異常,ip:%s,url:%s",ip,urlOrIp,e);
cache = false;
}
}
}
}
//數(shù)據(jù)不全,雖然成功但是不緩存
if(cache){
//存儲半個小時
VCache.setex(historyKey,bo,1800);
}對應的業(yè)務BO實體
@Data
public class IPBo implements Serializable {
private static final long serialVersionUID = -1052580448111049751L;
private String ip ;//ip
private String address;//地址
private String localAddress;//本地獲取的地址
private String baiduAddress;//百度獲取的地址
private String ua;//瀏覽器信息
private String lg;//語言
private String window;//系統(tǒng)
private String browser;//什么瀏覽器
private int type;//300:請輸入正確的Ip或域名,500:系統(tǒng)繁忙錯誤
//國家
private String nation;
//省
private String province;
//市
private String city;
//區(qū)
private String district;
//郵編
private String adcode;
//區(qū)號
private String areacode;
//精準區(qū)號
private String districtadcode;
//街道
private String street;
//門牌,可能為空字串
private String streetNumber;
private String message;
//定位建筑 集合
private List<PoiList> poiLists;
//是否精準定位
private Boolean location = Boolean.FALSE;
public IPBo(String ip,int type,String message) {
this.type = type;
this.message = message;
this.ip = ip;
}
public IPBo() {
}
@Data
public static class PoiList implements Serializable {
private static final long serialVersionUID = -1052180448111049751L;
//電話
private String tel;
//"岳麓區(qū)政協(xié)委員會",
private String title;
//"望岳街道金星北路一段517號",
private String address;
//地址的類型 政府機構及社會團體;政府機關;區(qū)縣級政府及事業(yè)單位",
private String category;
//坐標
private String lt;
//距離
private String distance;
public PoiList(){
}
public PoiList(Object e){
JSONObject elem;
if(null != e && e instanceof JSONObject){
elem = (JSONObject) e;
}else{
elem = JSONObject.fromObject(e);
}
this.tel = elem.optString("tel");
this.title = elem.optString("title");
this.address = elem.optString("address");
this.category = elem.optString("category");
if(StringUtils.contains(this.category,":")){
this.category = this.category.replaceAll(":","/");
}
this.distance = elem.optString("_distance");
JSONObject location = elem.optJSONObject("location");
if(null != location){
this.lt = String.format("%s/%s",location.optString("lat"),location.optString("lng"));
}
}
}
}JSON使用包為net.sf.json.JSONObject,地址為:http://suancuo.cn/blog/20.html
分享高德2個有用的地址:
https://www.amap.com/service/regeo?longitude=112.90131&latitude=28.20051
https://www.amap.com/service/getHistory?type=301
版權所屬:SO JSON在線解析
原文地址:http://suancuo.cn/blog/308.html
轉載時必須以鏈接形式注明原始出處及本聲明。
如果本文對你有幫助,那么請你贊助我,讓我更有激情的寫下去,幫助更多的人。
