高德地图API之IP定位

高德地图API之IP定位(替换你的KEY)

IP定位API:http://lbs.amap.com/api/webservice/guide/api/ipconfig

package com.gdmap.jopo.location;

public class Location {
	private String status;
	private String info;
	private String infocode;
	private String province;
	private String city;
	private String adcode;
	private String rectangle;

	public Location() {
		super();
	}

	public Location(String status, String info, String infocode,
			String province, String city, String adcode, String rectangle) {
		super();
		this.status = status;
		this.info = info;
		this.infocode = infocode;
		this.province = province;
		this.city = city;
		this.adcode = adcode;
		this.rectangle = rectangle;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	public String getInfocode() {
		return infocode;
	}

	public void setInfocode(String infocode) {
		this.infocode = infocode;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getAdcode() {
		return adcode;
	}

	public void setAdcode(String adcode) {
		this.adcode = adcode;
	}

	public String getRectangle() {
		return rectangle;
	}

	public void setRectangle(String rectangle) {
		this.rectangle = rectangle;
	}

	@Override
	public String toString() {
		return "Location [status=" + status + ", info=" + info + ", infocode="
				+ infocode + ", province=" + province + ", city=" + city
				+ ", adcode=" + adcode + ", rectangle=" + rectangle + "]";
	}

}
package com.gdmap.util.map;

import com.alibaba.fastjson.JSON;
import com.gdmap.jopo.location.Location;
import com.hbbc.util.HttpUtil;

public class LocationUtil {
	private static String URL = "http://restapi.amap.com/v3/ip";
	private static String key = "/*你的KEY*/";

	/**
	 * @author mzm
	 * @return Location 位置
	 */
	public static Location GetLocationByIP(){
		return GetLocationByIP(null);
	}
	
	/**
	 * @author mzm
	 * @param String IP:需要搜索的IP地址, (仅支持国内) 若用户不填写IP,则取客户http之中的请求来进行定位
	 * @return Location 位置
	 */
	public static Location GetLocationByIP(String IP) {
		URL += "?key=" + key;
		URL += "&ip=" + IP;
		
		String result = HttpUtil.callWebPage(URL, null);
		Location location = JSON.parseObject(result,Location.class);
		System.out.println(location.toString());
		return location;
	}
}

测试:

package com.hbbc.test;

import com.gdmap.util.map.LocationUtil;

public class Test {
	public static void main(String[] args) {
		LocationUtil.GetLocationByIP("114.247.50.2");
	}
}
   


输出结果:

Location [status=1, info=OK, infocode=10000, province=北京市, city=北京市, adcode=110000, rectangle=116.0119343,39.66127144;116.7829835,40.2164962]

你可能感兴趣的:(Java小知识)