MapUtil.java
package com.mxzhang.utils;
import java.io.BufferedReader;
import java.io.InputStreamReader;import java.net.URI;import java.net.URL;import java.net.URLConnection;import com.google.gson.Gson;
import com.mxzhang.wechat.lbs.bean.MapGpsConvertBean;import com.mxzhang.wechat.lbs.bean.MapRegeoBean;import com.mxzhang.wechat.lbs.bean.MapRegeoBean.AddressComponent;public class MapUtil {
public static String AMAP_WEB_KEY = "6901bd7712d0b3f97735ea904ec7dcd6"; public static String getHttpResponse(String serverUrl) { BufferedReader bf=null; StringBuffer result = null; try { URI uri = new URI(serverUrl); URL url = uri.toURL(); URLConnection connection = url.openConnection(); connection.setRequestProperty("Content-type", "text/html"); connection.setRequestProperty("Content-type", "application/json;charset=UTF-8"); connection.setRequestProperty("Accept-Charset", "utf-8"); connection.setRequestProperty("ContentType", "utf-8"); connection.connect(); result = new StringBuffer(); bf= new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8")); String line; while((line=bf.readLine())!=null) { result.append(line); } return result.toString(); } catch (Exception e) { e.printStackTrace(); }finally { try { if(bf!=null) { bf.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return null; } public static MapRegeoBean regeo(String lan, String lon){ String respString = getHttpResponse("http://restapi.amap.com/v3/geocode/regeo?output=json&location="+lon+","+lan+"&key="+AMAP_WEB_KEY); System.out.println(respString); return new Gson().fromJson(respString, MapRegeoBean.class); } public static MapGpsConvertBean convertGps(String lan, String lon){ String respString = getHttpResponse("http://restapi.amap.com/v3/assistant/coordinate/convert?output=json&coordsys=gps&locations="+lon+","+lan+"&key="+AMAP_WEB_KEY); System.out.println(respString); return new Gson().fromJson(respString, MapGpsConvertBean.class); } public static void main(String[] args) throws Exception { MapGpsConvertBean gpsRes = convertGps("30.4834","114.4197"); if(gpsRes.getStatus()!=null && "1".equals(gpsRes.getStatus())) { System.out.println(gpsRes.getLocations()); } MapRegeoBean res = regeo("30.481079101563","114.425219184028"); if(res.getStatus()!=null && "1".equals(res.getStatus())) { System.out.println(res.getRegeocode().getFormattedAddress()); AddressComponent addCom = res.getRegeocode().getAddressComponent(); System.out.println(addCom.getProvince()); } }}
实体类:MapBaseBean
package com.mxzhang.wechat.lbs.bean;
import com.mxzhang.wechat.lbs.bean.MapRegeoBean.Regeocode;
public class MapBaseBean {
private String status; private String info;public void setStatus(String status) {
this.status = status; }public String getStatus() {
return status; }public void setInfo(String info) {
this.info = info; }public String getInfo() {
return info; }}实体类:MapGpsConvertBean
package com.mxzhang.wechat.lbs.bean;
public class MapGpsConvertBean extends MapBaseBean {
private String locations;public void setLocations(String locations) {
this.locations = locations; }public String getLocations() {
return locations; }}
实体类:MapRegeoBean
package com.mxzhang.wechat.lbs.bean;
import java.util.List;
import org.codehaus.jackson.annotate.JsonProperty;
import com.google.gson.annotations.SerializedName;
public class MapRegeoBean extends MapBaseBean {
private String infocode; private Regeocode regeocode;public void setInfocode(String infocode) {
this.infocode = infocode; }public String getInfocode() {
return infocode; }public void setRegeocode(Regeocode regeocode) {
this.regeocode = regeocode; }public Regeocode getRegeocode() {
return regeocode; }public class Regeocode {
@SerializedName("formatted_address")
private String formattedAddress;private AddressComponent addressComponent;
private List<Pois> pois; private List<Roads> roads; private List<Roadinters> roadinters; private List<Aois> aois;public void setFormattedAddress(String formattedAddress) {
this.formattedAddress = formattedAddress; }public String getFormattedAddress() {
return formattedAddress; }public void setAddresscomponent(AddressComponent addressComponent) {
this.addressComponent = addressComponent; }public AddressComponent getAddressComponent() {
return addressComponent; }public void setPois(List<Pois> pois) {
this.pois = pois; }public List<Pois> getPois() {
return pois; }public void setRoads(List<Roads> roads) {
this.roads = roads; }public List<Roads> getRoads() {
return roads; }public void setRoadinters(List<Roadinters> roadinters) {
this.roadinters = roadinters; }public List<Roadinters> getRoadinters() {
return roadinters; }public void setAois(List<Aois> aois) {
this.aois = aois; }public List<Aois> getAois() {
return aois; } }public class AddressComponent {
private String country;
private String province; private String city; private String citycode; private String district; private String adcode; private String township; private String towncode;// private Neighborhood neighborhood;
// private Building building; // @JsonProperty("streetNumber") // private Streetnumber streetnumber; // @JsonProperty("businessAreas") // private List<Businessareas> businessareas; public void setCountry(String country) { this.country = country; }public String getCountry() {
return country; }public void setProvince(String province) {
this.province = province; }public String getProvince() {
return province; }public void setCity(String city) {
this.city = city; }public String getCity() {
return city; }public void setCitycode(String citycode) {
this.citycode = citycode; }public String getCitycode() {
return citycode; }public void setDistrict(String district) {
this.district = district; }public String getDistrict() {
return district; }public void setAdcode(String adcode) {
this.adcode = adcode; }public String getAdcode() {
return adcode; }public void setTownship(String township) {
this.township = township; }public String getTownship() {
return township; }public void setTowncode(String towncode) {
this.towncode = towncode; }public String getTowncode() {
return towncode; } }public class Pois {
private String id;
private String name; private String type; private String tel; private String direction; private String distance; private String location; private String address; private String poiweight; private String businessarea;public void setId(String id) {
this.id = id; }public String getId() {
return id; }public void setName(String name) {
this.name = name; }public String getName() {
return name; }public void setType(String type) {
this.type = type; }public String getType() {
return type; }public void setTel(String tel) {
this.tel = tel; }public String getTel() {
return tel; }public void setDirection(String direction) {
this.direction = direction; }public String getDirection() {
return direction; }public void setDistance(String distance) {
this.distance = distance; }public String getDistance() {
return distance; }public void setLocation(String location) {
this.location = location; }public String getLocation() {
return location; }public void setAddress(String address) {
this.address = address; }public String getAddress() {
return address; }public void setPoiweight(String poiweight) {
this.poiweight = poiweight; }public String getPoiweight() {
return poiweight; }public void setBusinessarea(String businessarea) {
this.businessarea = businessarea; }public String getBusinessarea() {
return businessarea; }}
public class Roads {
private String id;
private String name; private String direction; private String distance; private String location;public void setId(String id) {
this.id = id; }public String getId() {
return id; }public void setName(String name) {
this.name = name; }public String getName() {
return name; }public void setDirection(String direction) {
this.direction = direction; }public String getDirection() {
return direction; }public void setDistance(String distance) {
this.distance = distance; }public String getDistance() {
return distance; }public void setLocation(String location) {
this.location = location; }public String getLocation() {
return location; }}
public class Roadinters {
private String direction;
private String distance; private String location; @JsonProperty("first_id") private String firstId; @JsonProperty("first_name") private String firstName; @JsonProperty("second_id") private String secondId; @JsonProperty("second_name") private String secondName;public void setDirection(String direction) {
this.direction = direction; }public String getDirection() {
return direction; }public void setDistance(String distance) {
this.distance = distance; }public String getDistance() {
return distance; }public void setLocation(String location) {
this.location = location; }public String getLocation() {
return location; }public void setFirstId(String firstId) {
this.firstId = firstId; }public String getFirstId() {
return firstId; }public void setFirstName(String firstName) {
this.firstName = firstName; }public String getFirstName() {
return firstName; }public void setSecondId(String secondId) {
this.secondId = secondId; }public String getSecondId() {
return secondId; }public void setSecondName(String secondName) {
this.secondName = secondName; }public String getSecondName() {
return secondName; }}
public class Aois {
private String id;
private String name; private String adcode; private String location; private String area; private String distance; private String type;public void setId(String id) {
this.id = id; }public String getId() {
return id; }public void setName(String name) {
this.name = name; }public String getName() {
return name; }public void setAdcode(String adcode) {
this.adcode = adcode; }public String getAdcode() {
return adcode; }public void setLocation(String location) {
this.location = location; }public String getLocation() {
return location; }public void setArea(String area) {
this.area = area; }public String getArea() {
return area; }public void setDistance(String distance) {
this.distance = distance; }public String getDistance() {
return distance; }public void setType(String type) {
this.type = type; }public String getType() {
return type; }}
public class Neighborhood {
private String name;
private String type;public void setName(String name) {
this.name = name; }public String getName() {
return name; }public void setType(String type) {
this.type = type; }public String getType() {
return type; }}
public class Building {
private String name;
private String type;public void setName(String name) {
this.name = name; }public String getName() {
return name; }public void setType(String type) {
this.type = type; }public String getType() {
return type; }}
public class Streetnumber {
private String street;
private String number; private String location; private String direction; private String distance;public void setStreet(String street) {
this.street = street; }public String getStreet() {
return street; }public void setNumber(String number) {
this.number = number; }public String getNumber() {
return number; }public void setLocation(String location) {
this.location = location; }public String getLocation() {
return location; }public void setDirection(String direction) {
this.direction = direction; }public String getDirection() {
return direction; }public void setDistance(String distance) {
this.distance = distance; }public String getDistance() {
return distance; }}
public class Businessareas {
private String location;
private String name; private String id;public void setLocation(String location) {
this.location = location; }public String getLocation() {
return location; }public void setName(String name) {
this.name = name; }public String getName() {
return name; }public void setId(String id) {
this.id = id; }public String getId() {
return id; }}
}