Я как бы застрял здесь .. В основном я пытаюсь найти местоположение почтового индекса пользователя. У меня есть код для получения ZIP из GPS-координат, но я не могу получить никаких координат. Это код, который я до сих пор -Android - получить местоположение из сети один раз
Log.d(TAG, "Zip is going to be autoset");
new Thread() {
public void run() {
LocationManager locationManager;
String provider;
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
LocationListener locListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setAltitudeRequired(false);//true if required
criteria.setBearingRequired(false);//true if required
criteria.setCostAllowed(false);
criteria.setPowerRequirement(Criteria.POWER_LOW);
provider = locationManager.getBestProvider(criteria, true);//search for enabled provider
//locationManager.requestSingleUpdate(criteria, locListener, null);
Location location= locationManager.getLastKnownLocation(provider);
Log.d(TAG,"Lat - " + (location.getLatitude()) + " long - " + (location.getLongitude()));
latitude = (location.getLatitude());
longitude = (location.getLongitude());
Log.d(TAG, "Passing this link https://maps.googleapis.com/maps/api/geocode/xml?latlng="+latitude+","+longitude+"&sensor=true");
locationManager.removeUpdates(locListener);
try {
//Log.d(TAG, "Trying to access "+ "https://maps.googleapis.com/maps/api/geocode/xml?latlng="+location.getLatitude()+","+location.getLongitude()+"&sensor=true"
//Log.d(TAG, "Trying to access "+ "https://maps.googleapis.com/maps/api/geocode/xml?latlng=40.714224,-73.961452&sensor=true");
URL url = new URL("https://maps.googleapis.com/maps/api/geocode/xml?latlng="+latitude+","+longitude+"&sensor=true");
InputStream stream = url.openStream();
BufferedInputStream buf = new BufferedInputStream(stream);
StringBuilder sb = new StringBuilder();
while (true){
int data = buf.read();
if (data==-1){
break;
}else{
sb.append((char)data);
}
}
int zipEnd = sb.indexOf("</short_name><type>postal_code");
Log.d(TAG,"ZIP is "+sb.substring(zipEnd-6, zipEnd));
ZipCode = Integer.parseInt(sb.substring(zipEnd-6, zipEnd));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.run();
Последняя ошибка я получаю говорит, что я не могу создать обработчик внутри потока, который не называется looper.prepare.
Все, что мне нужно - это одно грубое обновление местоположения (расположение сети будет идеально), поэтому я могу получить от него почтовый индекс.
спасибо.
Вы смотрели в [это] (HTTP: // stackoverflow.com/questions/8598910/android-getting-network-gps-location-within-a-short-time-frame-10-seconds-max)? – Kgrover
приведенный выше код в действии? –
Вы говорите, что у вас проблема с Handler, но я не вижу, что вы использовали обработчик в любом месте кода вставки. это весь ваш код? и для почтового индекса вы можете использовать Geocoder, вам не нужно использовать что-то еще. – minhaz