diff options
author | Christian Schneppe <christian@pix-art.de> | 2016-09-17 22:04:56 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2016-09-17 22:04:56 +0200 |
commit | baf43ba41ad2e8382cc294153ebce37b0f6a5f97 (patch) | |
tree | 9501644771e9172642cdc344ddcb20aaeb26554d /src/main/java/de/pixart | |
parent | 66480f401476d974f903465289830342028185ec (diff) |
get addresses async in ShowLocation
Diffstat (limited to '')
-rw-r--r-- | src/main/java/de/pixart/messenger/ui/ShareLocationActivity.java | 4 | ||||
-rw-r--r-- | src/main/java/de/pixart/messenger/ui/ShowLocationActivity.java | 52 |
2 files changed, 41 insertions, 15 deletions
diff --git a/src/main/java/de/pixart/messenger/ui/ShareLocationActivity.java b/src/main/java/de/pixart/messenger/ui/ShareLocationActivity.java index 4e1e9c82e..d20981ac3 100644 --- a/src/main/java/de/pixart/messenger/ui/ShareLocationActivity.java +++ b/src/main/java/de/pixart/messenger/ui/ShareLocationActivity.java @@ -202,8 +202,8 @@ public class ShareLocationActivity extends Activity implements OnMapReadyCallbac protected String doInBackground(Double... params) { Geocoder geocoder = new Geocoder(mContext, Locale.getDefault()); - double latitude = params[0].doubleValue(); - double longitude = params[1].doubleValue(); + double latitude = params[0]; + double longitude = params[1]; List<Address> addresses = null; String address=""; diff --git a/src/main/java/de/pixart/messenger/ui/ShowLocationActivity.java b/src/main/java/de/pixart/messenger/ui/ShowLocationActivity.java index 4b13fd515..643cd6211 100644 --- a/src/main/java/de/pixart/messenger/ui/ShowLocationActivity.java +++ b/src/main/java/de/pixart/messenger/ui/ShowLocationActivity.java @@ -3,12 +3,15 @@ package de.pixart.messenger.ui; import android.Manifest; import android.app.ActionBar; import android.app.Activity; +import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.location.Address; import android.location.Geocoder; +import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; +import android.util.Log; import android.view.MenuItem; import android.view.View; import android.widget.TextView; @@ -32,6 +35,7 @@ public class ShowLocationActivity extends Activity implements OnMapReadyCallback private GoogleMap mGoogleMap; private LatLng mLocation; private String mLocationName; + private MarkerOptions options; class InfoWindowAdapter implements GoogleMap.InfoWindowAdapter { @@ -118,38 +122,60 @@ public class ShowLocationActivity extends Activity implements OnMapReadyCallback } } - private void markAndCenterOnLocation(LatLng location, String name) { - this.mGoogleMap.clear(); - MarkerOptions options = new MarkerOptions(); - options.position(location); - double longitude = mLocation.longitude; - double latitude = mLocation.latitude; + private static String getAddress(Context context, LatLng location) { + double longitude = location.longitude; + double latitude = location.latitude; + String address = ""; if (latitude != 0 && longitude != 0) { - Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault()); + Geocoder geoCoder = new Geocoder(context, Locale.getDefault()); try { List<Address> addresses = geoCoder.getFromLocation(latitude, longitude, 1); - - String address = ""; if (addresses != null) { Address Address = addresses.get(0); StringBuilder strAddress = new StringBuilder(""); - for (int i = 0; i < Address.getMaxAddressLineIndex(); i++) { strAddress.append(Address.getAddressLine(i)).append("\n"); } address = strAddress.toString(); address = address.substring(0, address.length()-1); //trim last \n - options.snippet(address); + } } catch (Exception e) { e.printStackTrace(); } } + return address; + } + + private void markAndCenterOnLocation(final LatLng location, String name) { + this.mGoogleMap.clear(); + options = new MarkerOptions(); + options.position(location); + double longitude = mLocation.longitude; + double latitude = mLocation.latitude; + this.mGoogleMap.setInfoWindowAdapter(new InfoWindowAdapter()); + + if (latitude != 0 && longitude != 0) { + new AsyncTask<Void, Void, Void>() { + String address = null; + + @Override + protected Void doInBackground(Void... params) { + address = getAddress(ShowLocationActivity.this, location); + return null; + } + + @Override + protected void onPostExecute(Void result) { + super.onPostExecute(result); + options.snippet(String.valueOf(address)); + mGoogleMap.addMarker(options).showInfoWindow(); + } + }.execute(); + } if (name != null) { options.title(name); } - this.mGoogleMap.setInfoWindowAdapter(new InfoWindowAdapter()); - this.mGoogleMap.addMarker(options).showInfoWindow(); this.mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(location, Config.DEFAULT_ZOOM)); } |