【问题标题】:how to get the latitude and longitude value from geoacast json如何从geoacast json中获取纬度和经度值
【发布时间】:2018-06-20 09:01:32
【问题描述】:

我真的一直在尝试获取纬度和经度值,但我不能,因为这个 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"
}

【问题讨论】:

  • 显然我真的成功了
  • 我在你的问题中看不到任何证据。请仔细阅读上面的链接,以了解为什么您说您确实尝试过的声明还远远不够……

标签: android json maps


【解决方案1】:

使用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;
    }

    }

【讨论】:

    【解决方案2】:

    对于 api 调用使用改造和 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();
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      • 1970-01-01
      • 2020-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多