【问题标题】:Places API parsing Lat/Lng using Java Client APIPlaces API 使用 Java Client API 解析 Lat/Lng
【发布时间】:2012-04-01 20:26:07
【问题描述】:

我想创建一个 android 应用程序,在用户当前位置附近的地图上绘制设施。到目前为止,除了 latlng 值之外,我能够执行请求并打印响应(例如 id、名称、附近区域等)。

我确信我得到了所需的响应,正如您从这条 logcat 消息中看到的那样:



以下是我如何访问/打印检索到的值:

-- 如注释行所述,访问“for”中的“name”的第一行代码有效,而第二行则无效。

@Override
protected void onPostExecute(PlacesList result) {
// TODO Auto-generated method stub
String text = "Result \n";

if (result!=null){
    for(Place place: result.results) {
        // This works   
        text = text + place.name +"\n";

        // This doesn't
        text = text + place.geometry.location.lat +"\n";
    }
    txt1.setText(text);
}
setProgressBarIndeterminateVisibility(false);
}


我猜这个问题可能是由模型(来源:link)的处理方式引起的:

-- 需要注意的是,声明(并且不加注释)public Geometry geometry;完全阻止我检索任何响应值(id、名称等)。

public class PlacesList {
@Key
public String status;

@Key
public List<Place> results;
}


public class Place {
    @Key
    public String id;

    @Key
    public String name;

    @Key
    public String reference;

    @Key
    public String vicinity;

    @Key
    public String icon;

    // Leaving this line prevents me from retrieving any of the above values    
    @Key
    public Geometry geometry;

    // On the other hand, declaring non-existing elements (elements that are not
    // included on the Place Search response) does not break the program 
    // at all (i.e. values from valid elements such as id, name, etc. can be retrieved) 
    @Key
    public String testFakeElement;

    @Override
    public String toString() {
        return name + " - " + id + " - " + reference;
    }
}


public class Geometry {
    @Key
    public Location location;

    @Override
    public String toString() {
        return location.toString();
    }

    public class Location {
        @Key
        public double lat;

        @Key
        public double lng;

        @Override
        public String toString() {
            return Double.toString(lat) + ", " + Double.toString(lng);
        }
    }
}

【问题讨论】:

  • @DilSe 现在可以使用了。在下面检查我的答案。

标签: android httpresponse google-api-java-client google-places-api


【解决方案1】:

我终于让它工作了。
正如我所想的那样,模型本身就是阻止我检索经纬度的原因。
此博客中的信息有所帮助:link

我取出了 Geometry 类,只是在 Place 类中添加了以下内容:

@Key
public Geometry geometry;

public static class Geometry {   
    @Key
    public Location location;
}

public static class Location {
    @Key
    public double lat;

    @Key
    public double lng;

    public String toString() {
        return Double.toString(lat) + ", " + Double.toString(lng);
    }
}

【讨论】:

    猜你喜欢
    • 2012-03-11
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    相关资源
    最近更新 更多