Android: Odwróć geokodowanie-getFromLocation

Próbuję uzyskać adres oparty na long / lat. wygląda na to, że coś takiego powinno zadziałać?

Geocoder myLocation = Geocoder(Locale.getDefault());
    List myList = myLocation.getFromLocation(latPoint,lngPoint,1);

Problem polega na tym, że ciągle otrzymuję: metoda Geocoder (Locale) jest niezdefiniowana dla typu savemaplocation

Każda pomoc byłaby pomocna. Dziękuję.

Dzięki, próbowałem kontekstu, locale jeden pierwszy, i to nie powiodło się i patrzyłem na niektórych innych konstruktorów(widziałem jeden, który wspomniał tylko locale). Niezależnie,

Zrobił nie działa, ponieważ wciąż otrzymuję: metoda Geocoder (Context, Locale) jest niezdefiniowana dla typu savemaplocation

Mam : import android.miejsce.Geocoder;

Author: Matt, 2009-01-23

8 answers

Poniższy fragment kodu robi to za mnie (lat i lng są podwojone zadeklarowane powyżej tego bitu):

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
 63
Author: Wilfred Knievel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-01-25 00:58:21

Oto pełny przykładowy kod wykorzystujący wątek i obsługę, aby uzyskać odpowiedź Geocodera bez blokowania interfejsu użytkownika.

Procedura wywołania Geocodera, może być zlokalizowana w klasie pomocniczej

public static void getAddressFromLocation(
        final Location location, final Context context, final Handler handler) {
    Thread thread = new Thread() {
        @Override public void run() {
            Geocoder geocoder = new Geocoder(context, Locale.getDefault());   
            String result = null;
            try {
                List<Address> list = geocoder.getFromLocation(
                        location.getLatitude(), location.getLongitude(), 1);
                if (list != null && list.size() > 0) {
                    Address address = list.get(0);
                    // sending back first address line and locality
                    result = address.getAddressLine(0) + ", " + address.getLocality();
                }
            } catch (IOException e) {
                Log.e(TAG, "Impossible to connect to Geocoder", e);
            } finally {
                Message msg = Message.obtain();
                msg.setTarget(handler);
                if (result != null) {
                    msg.what = 1;
                    Bundle bundle = new Bundle();
                    bundle.putString("address", result);
                    msg.setData(bundle);
                } else 
                    msg.what = 0;
                msg.sendToTarget();
            }
        }
    };
    thread.start();
}
Tutaj jest wywołanie tej procedury Geocoder w aktywności UI:
getAddressFromLocation(mLastKownLocation, this, new GeocoderHandler());

I obsługa wyświetlania wyników w interfejsie użytkownika:

private class GeocoderHandler extends Handler {
    @Override
    public void handleMessage(Message message) {
        String result;
        switch (message.what) {
        case 1:
            Bundle bundle = message.getData();
            result = bundle.getString("address");
            break;
        default:
            result = null;
        }
        // replace by what you need to do
        myLabel.setText(result);
    }   
}

Nie zapomnij umieścić poniższego pozwolenia w swoim Manifest.xml

<uses-permission android:name="android.permission.INTERNET" />
 42
Author: EricLarch,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-12 22:35:04

Wygląda na to, że dzieją się tu dwie rzeczy.

1) przegapiłeś słowo kluczowe new przed wywołaniem konstruktora.

2) parametr, który PRZEKAZUJESZ do konstruktora Geokodera, jest nieprawidłowy. Przechodzisz w Locale gdzie oczekuje Context.

Istnieją dwa konstruktory Geocoder, z których oba wymagają Context, a jeden również przyjmuje Locale:

Geocoder(Context context, Locale locale)
Geocoder(Context context)

Rozwiązanie

Zmodyfikuj swój kod tak, aby pasował do poprawnego kontekstu i include new and you should be good to go.

Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());   
List<Address> myList = myLocation.getFromLocation(latPoint, lngPoint, 1);

Uwaga

Jeśli nadal masz problemy, może to być problem z pozwoleniem. Geokodowanie domyślnie używa Internetu do wyszukiwania, więc Twoja aplikacja będzie wymagała znacznika INTERNET uses-permission w manifeście.

Dodaj następujący węzeł uses-permission wewnątrz manifest węzła manifestu.

<uses-permission android:name="android.permission.INTERNET" />
 35
Author: Reto Meier,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-01-25 19:27:22

Powodem tego jest nieistniejąca usługa Backend:

Klasa Geocoder wymaga usługi backend, która nie jest zawarta w core Android framework. Metody zapytań Geocoder zwróci pustą listę, jeśli nie ma usługi backend na platformie.

 8
Author: Ingo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-27 17:08:40

Najpierw uzyskaj szerokość i długość geograficzną używając klasy Location i LocationManager. Teraz wypróbuj poniższy kod, aby uzyskać informacje o mieście, adresie

double latitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
    List<Address> addresses = gc.getFromLocation(lat, lng, 1);
    StringBuilder sb = new StringBuilder();
    if (addresses.size() > 0) {
        Address address = addresses.get(0);
        for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
            sb.append(address.getAddressLine(i)).append("\n");
            sb.append(address.getLocality()).append("\n");
            sb.append(address.getPostalCode()).append("\n");
            sb.append(address.getCountryName());
    }

City info jest teraz w sb. Teraz Konwertuj SB na łańcuch znaków (używając sb.toString ()).

 4
Author: Antony,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-13 05:02:41

Cóż, wciąż jestem zakłopotany. Więc tu jest więcej kodu.

Zanim opuszczę moją mapę, dzwonię SaveLocation(myMapView,myMapController); to jest to, co kończy się nazywaniem mojej informacji geokodowania.

Ale ponieważ getFromLocation może rzucić IOException, musiałem wykonać następujące czynności, aby wywołać SaveLocation

try
{
    SaveLocation(myMapView,myMapController);
}
catch (IOException e) 
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Potem muszę zmienić SaveLocation mówiąc, że rzuca IOExceptions:

 public void SaveLocation(MapView mv, MapController mc) throws IOException{
    //I do this : 
    Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());   
    List myList = myLocation.getFromLocation(latPoint, lngPoint, 1);
//...
    }

I za każdym razem się psuje.

 2
Author: Chrispix,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-27 14:21:06

Słyszałem, że Geocoder jest po stronie buggy. Niedawno ułożyła przykładową aplikację, która wykorzystuje usługę Google http geocoding do wyszukiwania lokalizacji z lat / long. Zapraszam do sprawdzenia tutaj

Http://www.smnirven.com/?p=39

 0
Author: ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-10-09 05:55:59

Użyj tego

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
 0
Author: Sojan Ks,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-07 14:35:08