如何从geoacast json获取纬度和经度值

时间:2018-06-20 09:01:33

标签: android json maps

我真的是在尝试获取纬度和经度值,但是我不能,因为此json确实是嵌套的。我正在使用android是他们通过提供名称来获取城市的经度和纬度的另一种方法?还是可以让我从该JSON中获取纬度和经度:

    {
    "results": [
        {
            "address_components": [
                {
                    "long_name": "Google Building 41",
                    "short_name": "Google Building 41",
                    "types": [
                        "premise"
                    ]
                },
                {
                    "long_name": "1600",
                    "short_name": "1600",
                    "types": [
                        "street_number"
                    ]
                },
                {
                    "long_name": "Amphitheatre Parkway",
                    "short_name": "Amphitheatre Pkwy",
                    "types": [
                        "route"
                    ]
                },
                {
                    "long_name": "Mountain View",
                    "short_name": "Mountain View",
                    "types": [
                        "locality",
                        "political"
                    ]
                },
                {
                    "long_name": "Santa Clara County",
                    "short_name": "Santa Clara County",
                    "types": [
                        "administrative_area_level_2",
                        "political"
                    ]
                },
                {
                    "long_name": "California",
                    "short_name": "CA",
                    "types": [
                        "administrative_area_level_1",
                        "political"
                    ]
                },
                {
                    "long_name": "United States",
                    "short_name": "US",
                    "types": [
                        "country",
                        "political"
                    ]
                },
                {
                    "long_name": "94043",
                    "short_name": "94043",
                    "types": [
                        "postal_code"
                    ]
                }
            ],
            "formatted_address": "Google Building 41, 1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 37.4228775,
                        "lng": -122.085133
                    },
                    "southwest": {
                        "lat": 37.4221145,
                        "lng": -122.0860002
                    }
                },
                "location": {
                    "lat": 37.4224082,
                    "lng": -122.0856086
                },
                "location_type": "ROOFTOP",
                "viewport": {
                    "northeast": {
                        "lat": 37.4238449802915,
                        "lng": -122.0842176197085
                    },
                    "southwest": {
                        "lat": 37.4211470197085,
                        "lng": -122.0869155802915
                    }
                }
            },
            "place_id": "ChIJxQvW8wK6j4AR3ukttGy3w2s",
            "types": [
                "premise"
            ]
        }
    ],
    "status": "OK"
}

2 个答案:

答案 0 :(得分:0)

使用http://www.jsonschema2pojo.org/简化json

----------------------------------- com.example.AddressComponent.java ----- ------------------------------

    package com.example;

    import java.util.List;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    public class AddressComponent {

    @SerializedName("long_name")
    @Expose
    private String longName;
    @SerializedName("short_name")
    @Expose
    private String shortName;
    @SerializedName("types")
    @Expose
    private List<String> types = null;

    public String getLongName() {
    return longName;
    }

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

    public String getShortName() {
    return shortName;
    }

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

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

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

    }

----------------------------------- com.example.Bounds.java ----- ------------------------------

    package com.example;

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    public class Bounds {

    @SerializedName("northeast")
    @Expose
    private Northeast northeast;
    @SerializedName("southwest")
    @Expose
    private Southwest southwest;

    public Northeast getNortheast() {
    return northeast;
    }

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

    public Southwest getSouthwest() {
    return southwest;
    }

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

    }

----------------------------------- com.example.Example.java ----- ------------------------------

    package com.example;

    import java.util.List;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    public class Example {

    @SerializedName("results")
    @Expose
    private List<Result> results = null;
    @SerializedName("status")
    @Expose
    private String status;

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

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

    public String getStatus() {
    return status;
    }

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

    }

----------------------------------- com.example.Geometry.java ----- ------------------------------

    package com.example;

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    public class Geometry {

    @SerializedName("bounds")
    @Expose
    private Bounds bounds;
    @SerializedName("location")
    @Expose
    private Location location;
    @SerializedName("location_type")
    @Expose
    private String locationType;
    @SerializedName("viewport")
    @Expose
    private Viewport viewport;

    public Bounds getBounds() {
    return bounds;
    }

    public void setBounds(Bounds bounds) {
    this.bounds = bounds;
    }

    public Location getLocation() {
    return location;
    }

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

    public String getLocationType() {
    return locationType;
    }

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

    public Viewport getViewport() {
    return viewport;
    }

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

    }

----------------------------------- com.example.Location.java ----- ------------------------------

    package com.example;

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    public class Location {

    @SerializedName("lat")
    @Expose
    private Double lat;
    @SerializedName("lng")
    @Expose
    private Double lng;

    public Double getLat() {
    return lat;
    }

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

    public Double getLng() {
    return lng;
    }

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

    }

----------------------------------- com.example.Northeast.java ----- ------------------------------

    package com.example;

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    public class Northeast {

    @SerializedName("lat")
    @Expose
    private Double lat;
    @SerializedName("lng")
    @Expose
    private Double lng;

    public Double getLat() {
    return lat;
    }

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

    public Double getLng() {
    return lng;
    }

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

    }

----------------------------------- com.example.Northeast_.java ----- ------------------------------

    package com.example;

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    public class Northeast_ {

    @SerializedName("lat")
    @Expose
    private Double lat;
    @SerializedName("lng")
    @Expose
    private Double lng;

    public Double getLat() {
    return lat;
    }

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

    public Double getLng() {
    return lng;
    }

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

    }

----------------------------------- com.example.Result.java ----- ------------------------------

    package com.example;

    import java.util.List;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    public class Result {

    @SerializedName("address_components")
    @Expose
    private List<AddressComponent> addressComponents = null;
    @SerializedName("formatted_address")
    @Expose
    private String formattedAddress;
    @SerializedName("geometry")
    @Expose
    private Geometry geometry;
    @SerializedName("place_id")
    @Expose
    private String placeId;
    @SerializedName("types")
    @Expose
    private List<String> types = null;

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

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

    public String getFormattedAddress() {
    return formattedAddress;
    }

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

    public Geometry getGeometry() {
    return geometry;
    }

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

    public String getPlaceId() {
    return placeId;
    }

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

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

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

    }

----------------------------------- com.example.Southwest.java ----- ------------------------------

    package com.example;

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    public class Southwest {

    @SerializedName("lat")
    @Expose
    private Double lat;
    @SerializedName("lng")
    @Expose
    private Double lng;

    public Double getLat() {
    return lat;
    }

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

    public Double getLng() {
    return lng;
    }

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

    }

----------------------------------- com.example.Southwest_.java ----- ------------------------------

    package com.example;

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    public class Southwest_ {

    @SerializedName("lat")
    @Expose
    private Double lat;
    @SerializedName("lng")
    @Expose
    private Double lng;

    public Double getLat() {
    return lat;
    }

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

    public Double getLng() {
    return lng;
    }

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

    }

----------------------------------- com.example.Viewport.java ----- ------------------------------

    package com.example;

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    public class Viewport {

    @SerializedName("northeast")
    @Expose
    private Northeast_ northeast;
    @SerializedName("southwest")
    @Expose
    private Southwest_ southwest;

    public Northeast_ getNortheast() {
    return northeast;
    }

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

    public Southwest_ getSouthwest() {
    return southwest;
    }

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

    }

答案 1 :(得分:0)

对于api调用,将翻新和esay用于json解析。 将以下依赖项添加到应用程序级别的gradle文件中。

implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'

之后,使用Rebo pojo puligns或json响应的所有pojo类 http://www.jsonschema2pojo.org/这个网站。

之后就是改造对象类。在该类中,访问地图方向api并放置api。

public class ApiClient {
private final static String BASE_URL = "https://maps.googleapis.com/maps/api/directions/";
private final static String PLACE_API_BASE_URL="https://maps.googleapis.com/maps/api/place/";
public static ApiClient apiClient;
private Retrofit retrofit = null;
public static ApiClient getInstance() {
    if (apiClient == null) {
        apiClient = new ApiClient();
    }
    return apiClient;
}

//private static Retrofit storeRetrofit = null;

public Retrofit getClient() {
    return getClient(null);
}

public Retrofit getPlaceClient() {
    return getPlaceClient(null);
}

private Retrofit getPlaceClient(Context context) {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder client = new OkHttpClient.Builder();
    client.readTimeout(60, TimeUnit.SECONDS);
    client.writeTimeout(60, TimeUnit.SECONDS);
    client.connectTimeout(60, TimeUnit.SECONDS);
    client.addInterceptor(interceptor);
    client.addInterceptor(new Interceptor() {
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            Request request = chain.request();

            return chain.proceed(request);
        }
    });

      retrofit = new Retrofit.Builder()
            .baseUrl(PLACE_API_BASE_URL)
            .client(client.build())
            .addConverterFactory(GsonConverterFactory.create())
            .build();


    return retrofit;

}


  private Retrofit getClient(final Context context) {

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder client = new OkHttpClient.Builder();
    client.readTimeout(60, TimeUnit.SECONDS);
    client.writeTimeout(60, TimeUnit.SECONDS);
    client.connectTimeout(60, TimeUnit.SECONDS);
    client.addInterceptor(interceptor);
    client.addInterceptor(new Interceptor() {
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            Request request = chain.request();

            return chain.proceed(request);
        }
    });

    retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(client.build())
            .addConverterFactory(GsonConverterFactory.create())
            .build();


    return retrofit;
}
}

之后,如下所示进行api调用界面

public interface ApiInterface {
@GET("json")
Call<Response> getRouteData(@Query("origin") String from, @Query("destination") String to, @Query("key") String key);
}

然后将这个api称为活动或片段。

 private void getLocationData() {
    ApiInterface apiInterface = ApiClient.getInstance().getClient().create(ApiInterface.class);
    Call<Response> responseCall = apiInterface.getRouteData(mEtFrom.getText().toString().trim(), mEtTo.getText().toString().trim(), getString(R.string.direction_key));
    displayProgress();
    responseCall.enqueue(new Callback<Response>() {
        @Override
        public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
            if (response.isSuccessful() && response != null && response.body() != null) {
                closeProgreess();
                Response response1 = response.body();
                /*for (RoutesItem route : response1.getRoutes()) {
                    tLat = route.getBounds().getSouthwest().getLat();
                    tLongvalue = route.getBounds().getSouthwest().getLng();
                    fLat=route.getBounds().getNortheast().getLat();
                    fLongvalue=route.getBounds().getNortheast().getLng();
                }*/
                  Intent intent = new Intent(UserPlaceInput.this, MapsActivity.class);
                intent.putExtra("From", mEtFrom.getText().toString().trim());
                intent.putExtra("To", mEtTo.getText().toString().trim());
                intent.putExtra("Response", response1);
                intent.putExtra("LogTag",LOG_TAG);
                startActivity(intent);
            }
        }

        @Override
        public void onFailure(Call<Response> call, Throwable t) {
            closeProgreess();
        }
    });
}