yandex定位 也就是通过坐标获取地理位置
直接上代码
1.首先先创建一个实体类名字gsm_cells
/**
* 一组对象,每个对象描述一个单元格
*/
public class gsm_cells {
private int countrycode;//国家代码
private int operatorid;//移动网络代码。
private int cellid;//Cell id
private int lac;//位置代码
private int signal_strength;//信号强度
private int age;//接收数据以来的时间(以毫秒为单位)
public int getCountrycode() {
return countrycode;
}
public void setCountrycode(int countrycode) {
this.countrycode = countrycode;
}
public int getOperatorid() {
return operatorid;
}
public void setOperatorid(int operatorid) {
this.operatorid = operatorid;
}
public int getCellid() {
return cellid;
}
public void setCellid(int cellid) {
this.cellid = cellid;
}
public int getLac() {
return lac;
}
public void setLac(int lac) {
this.lac = lac;
}
public int getSignal_strength() {
return signal_strength;
}
public void setSignal_strength(int signal_strength) {
this.signal_strength = signal_strength;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public gsm_cells(int countrycode, int operatorid, int cellid, int lac, int signal_strength, int age) {
super();
this.countrycode = countrycode;
this.operatorid = operatorid;
this.cellid = cellid;
this.lac = lac;
this.signal_strength = signal_strength;
this.age = age;
}
}
2.创建接口实现类wifi_networks
/**
* 一组对象,每个对象描述一个Wi-Fi接入点
*/
public class wifi_networks {
private String mac;//没有分隔符的字符表示中的MAC地址
private int signal_strength;//在移动设备的位置处测量的信号强度
private int age;//接收数据以来的时间(以毫秒为单位)
public String getMac() {
return mac;
}
public void setMac(String mac) {
this.mac = mac;
}
public int getSignal_strength() {
return signal_strength;
}
public void setSignal_strength(int signal_strength) {
this.signal_strength = signal_strength;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public wifi_networks(String mac, int signal_strength, int age) {
super();
this.mac = mac;
this.signal_strength = signal_strength;
this.age = age;
}
}
3.我需要的是map类型数据 创建SendJson类
public class SendJson {
private Map common;
private Map ip;
public Map getCommon() {
return common;
}
public void setCommon(Map common) {
this.common = common;
}
public Map getIp() {
return ip;
}
public void setIp(Map ip) {
this.ip = ip;
}
public SendJson(Map common, Map ip) {
super();
this.common = common;
this.ip = ip;
}
}
4.主方法Positioning
public class Positioning {
public static void main(String[] args) throws UnsupportedEncodingException {
@SuppressWarnings("deprecation")
CloseableHttpClient httpClient = HttpClients.createDefault();
Map commonChildMap = new HashMap();
//纬度的API版本
commonChildMap.put("version", "1.0");
//定位器API访问密钥
commonChildMap.put("api_key", "这里是你的key");
Map ipChildMap = new HashMap();
ipChildMap.put("address_v4", "95.108.173.231");
JSONObject json = JSONObject.fromObject(new SendJson(commonChildMap,ipChildMap)); //,gsmCells,wifiNetworks
System.out.println(json);
HttpGet httpGet = new HttpGet("https://api.lbs.yandex.net/geolocation?json="+URLEncoder.encode(json.toString(), "UTF-8") );
httpGet.setHeader("Content-Type", "application/json;charset=UTF-8");
// post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
// 响应模型
CloseableHttpResponse response = null;
try {
// 由客户端执行(发送)Post请求
response = httpClient.execute(httpGet);
// 从响应模型中获取响应实体
HttpEntity responseEntity = response.getEntity();
System.out.println("响应状态为:" + response.getStatusLine());
if (responseEntity != null) {
System.out.println("响应内容长度为:" + responseEntity.getContentLength());
System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
5.成功后控制台输出200状态码
响应状态为:HTTP/1.1 200 Ok
响应内容长度为:226
响应内容为:{
“position”:
{
“latitude”:55.75321578979492,
“longitude”:37.62250518798828,
“altitude”:0.0,
“precision”:100000.0,
“altitude_precision”:30.0,
“type”:“ip”
}
}