Размещает анализ API Lat / Lng с использованием Java Client API - PullRequest
0 голосов
/ 02 апреля 2012

Я хочу создать приложение для Android, которое будет отображать заведения на карте рядом с текущим местоположением пользователя. На данный момент я могу выполнить запрос и распечатать ответ (например, идентификатор, имя, район и т. Д.), За исключением значений lat и lng .

Я уверен, что получаю необходимый ответ, как вы можете видеть из этого сообщения logcat:

enter image description here

Ниже описано, как я могу получить доступ / распечатать полученные значения:

- Как указано в закомментированных строках, первая строка кода, которая обращается к «имени» внутри «для», работает, а вторая - нет.

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


Я предполагаю, что проблема, вероятно, вызвана тем, как обрабатывается модель (источник: ссылка ):

- Может быть важно отметить, что объявление (и оставление без комментариев) public Geometry geometry; полностью запрещает мне получать любые значения ответа (id, name и т. Д.).

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 Ответ

1 голос
/ 03 апреля 2012

Я наконец-то заработал.Как я и думал, сама модель - это то, что мешает мне найти широту / долготу.Помогла информация из этого блога: ссылка

Я вынул класс 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);
    }
}
...