为了做用户数字账单,不得不定位一下城市。因为客户遍布全球,不太容易兼容。国内的接口都是大陆范围,经过探索找到了一个好库。GEOIP2,链接放底部了。下面是主要调用函数,大家用的时候参考吧。
/**
* 根据ip获取国际城市的经纬度和名称等信息
* @param ip
* @return JSONObject {'city':'美国纽约',x:120.3,y:128.4}
* @throws IOException
* @throws GeoIp2Exception
*/
public static JSONObject getCityInfoByIPGlobal(String ip) throws IOException, GeoIp2Exception {
JSONObject cityInfo = new JSONObject();
if(IPUtils.ipIsInner(ip)) {
cityInfo.put("city", "北京");
cityInfo.put("x",2);//Long
cityInfo.put("y", 6);//Lat
}else {
// try {
// JSONObject c1 = getCity(ip);
// String city = c1.getJSONObject("address_detail").getString("province")+c1.getJSONObject("address_detail").getString("city");
// cityInfo.put("city", city);
// cityInfo.put("x",c1.getJSONObject("point").getDoubl