查询参数在改造中用?在Android谷歌地图

时间:2017-11-13 09:21:43

标签: android google-maps retrofit

当我在改造中使用查询参数访问链接网址(如第一个链接)并且我获得了第二个链接网址https://maps.googleapis.com/maps/api/geocode/json?latlng=11.531887,104.937726

之类的链接网址时,我对改造有问题

https://maps.googleapis.com/maps/api/geocode/json?geocode=json&latlng=11.531887,104.937726

public interface AddressService{
        @GET("geocode/json")
        Call<AddressResponse> getAddressWithCoordinator(@Query("latlng") String latlng);
}

谢谢你!

1 个答案:

答案 0 :(得分:1)

创建一个latlong类

class LatLng {
  private double lat;
  private double lng;

  @Override public String toString() {
    return String.format("%.1f,%.1f", lat, lng);
  }
}

服务如

public interface AddressService{
    @GET("geocode/json")
    Call<AddressResponse> getAddressWithCoordinator(@Query("latlng") LatLng latlng);
}

致电请求

Call<AddressResponse> requestCall =  restAdapter.getAddressWithCoordinator(LatLng.valueOf(31.26, -94.74));

模型应该看起来像

import com.google.gson.annotations.SerializedName;

import java.util.List;

public class AddressResponse{

    @SerializedName("results")
    private List<ResultsItem> results;

    @SerializedName("status")
    private String status;

    public void setResults(List<ResultsItem> results){
        this.results = results;
    }

    public List<ResultsItem> getResults(){
        return results;
    }

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

    public String getStatus(){
        return status;
    }

    public class AddressComponentsItem{

        @SerializedName("types")
        private List<String> types;

        @SerializedName("short_name")
        private String shortName;

        @SerializedName("long_name")
        private String longName;

        public void setTypes(List<String> types){
            this.types = types;
        }

        public List<String> getTypes(){
            return types;
        }

        public void setShortName(String shortName){
            this.shortName = shortName;
        }

        public String getShortName(){
            return shortName;
        }

        public void setLongName(String longName){
            this.longName = longName;
        }

        public String getLongName(){
            return longName;
        }
    }

    public class Geometry{

        @SerializedName("viewport")
        private Viewport viewport;

        @SerializedName("location")
        private Location location;

        @SerializedName("location_type")
        private String locationType;

        public void setViewport(Viewport viewport){
            this.viewport = viewport;
        }

        public Viewport getViewport(){
            return viewport;
        }

        public void setLocation(Location location){
            this.location = location;
        }

        public Location getLocation(){
            return location;
        }

        public void setLocationType(String locationType){
            this.locationType = locationType;
        }

        public String getLocationType(){
            return locationType;
        }
    }


    public class Location{

        @SerializedName("lng")
        private double lng;

        @SerializedName("lat")
        private double lat;

        public void setLng(double lng){
            this.lng = lng;
        }

        public double getLng(){
            return lng;
        }

        public void setLat(double lat){
            this.lat = lat;
        }

        public double getLat(){
            return lat;
        }
    }


    public class ResultsItem{

        @SerializedName("formatted_address")
        private String formattedAddress;

        @SerializedName("types")
        private List<String> types;

        @SerializedName("geometry")
        private Geometry geometry;

        @SerializedName("address_components")
        private List<AddressComponentsItem> addressComponents;

        @SerializedName("place_id")
        private String placeId;

        public void setFormattedAddress(String formattedAddress){
            this.formattedAddress = formattedAddress;
        }

        public String getFormattedAddress(){
            return formattedAddress;
        }

        public void setTypes(List<String> types){
            this.types = types;
        }

        public List<String> getTypes(){
            return types;
        }

        public void setGeometry(Geometry geometry){
            this.geometry = geometry;
        }

        public Geometry getGeometry(){
            return geometry;
        }

        public void setAddressComponents(List<AddressComponentsItem> addressComponents){
            this.addressComponents = addressComponents;
        }

        public List<AddressComponentsItem> getAddressComponents(){
            return addressComponents;
        }

        public void setPlaceId(String placeId){
            this.placeId = placeId;
        }

        public String getPlaceId(){
            return placeId;
        }
    }

    public class Viewport{

        @SerializedName("southwest")
        private Location southwest;

        @SerializedName("northeast")
        private Location northeast;

        public void setSouthwest(Location southwest){
            this.southwest = southwest;
        }

        public Location getSouthwest(){
            return southwest;
        }

        public void setNortheast(Location northeast){
            this.northeast = northeast;
        }

        public Location getNortheast(){
            return northeast;
        }
    }


}