Places API使用Java Client API解析Lat / Lng

时间:2012-04-01 20:26:08

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

我想创建一个Android应用程序,在用户当前位置附近的地图上绘制场所。截至目前,我能够执行请求并打印响应(例如id,name,near等),但 lat lng 值除外。

我确信我正在从这个logcat消息中看到所需的响应:

enter image description here

以下是我访问/打印检索值的方法:

- 如注释行中所述,访问'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)的处理方式引起的:

     - 重要的是要注意声明(并保留未注释)公共几何几何; 完全阻止我检索任何响应值(id,name等)。< / em>的

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);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我终于开始工作了。
正如我所想,模型本身就是阻止我检索lat / lang的原因。
来自此博客的信息有助于: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);
    }
}