11package mil .nga .giat .mage .map ;
22
33import android .content .Context ;
4+ import android .location .Address ;
5+ import android .location .Geocoder ;
46import android .os .AsyncTask ;
57import android .util .Log ;
68import android .widget .Toast ;
79
8- import com .bricolsoftconsulting .geocoderplus .Address ;
9- import com .bricolsoftconsulting .geocoderplus .Geocoder ;
1010import com .google .android .gms .maps .CameraUpdateFactory ;
1111import com .google .android .gms .maps .GoogleMap ;
12+ import com .google .android .gms .maps .model .CameraPosition ;
1213import com .google .android .gms .maps .model .LatLng ;
13- import com .google .android .gms .maps .model .LatLngBounds ;
1414import com .google .android .gms .maps .model .Marker ;
1515import com .google .android .gms .maps .model .MarkerOptions ;
1616
@@ -25,6 +25,9 @@ public class GeocoderTask extends AsyncTask<String, Void, GeocoderTask.SearchRes
2525
2626 private static final String LOG_NAME = GeocoderTask .class .getName ();
2727
28+ private static final int MAX_ADDRESS_LINES = 3 ;
29+ private static final int MAX_ADDRESS_ZOOM = 18 ;
30+
2831 private Context context ;
2932
3033 // reference to map
@@ -53,30 +56,30 @@ protected SearchResults doInBackground(String... params) {
5356 .title ("MGRS" )
5457 .snippet (searchString );
5558
56- results .bounds = new LatLngBounds ( position , position ) ;
59+ results .zoom = 18 ;
5760 } catch (ParseException e ) {
5861 Log .e (LOG_NAME , "Problem parsing mgrs string" , e );
5962 }
6063 } else {
6164 // Creating an instance of Geocoder class
62- Geocoder geocoder = new Geocoder ();
65+ Geocoder geocoder = new Geocoder (context );
6366
6467 try {
6568 List <Address > addresses = geocoder .getFromLocationName (searchString , 1 );
6669 if (addresses != null && addresses .size () > 0 ) {
6770 Address address = addresses .get (0 );
6871
69- LatLng southWestlatLng = new LatLng (address .getViewPort ().getSouthWest ().getLatitude (), address .getViewPort ().getSouthWest ().getLongitude ());
70- LatLng northEastlatLng = new LatLng (address .getViewPort ().getNorthEast ().getLatitude (), address .getViewPort ().getNorthEast ().getLongitude ());
71- results .bounds = new LatLngBounds (southWestlatLng , northEastlatLng );
72+ int addressLines = address .getMaxAddressLineIndex () + 1 ;
73+ results .zoom = MAX_ADDRESS_ZOOM - ((MAX_ADDRESS_LINES - addressLines ) * 2 );
7274
7375 results .markerOptions = new MarkerOptions ()
7476 .position (new LatLng (address .getLatitude (), address .getLongitude ()))
7577 .title (searchString )
76- .snippet (address .getFormattedAddress ());
78+ .snippet (address .getAddressLine (0 ));
79+
7780 }
7881 } catch (IOException e ) {
79- Log .e (LOG_NAME , "Problem executing search." );
82+ Log .e (LOG_NAME , "Problem executing search." , e );
8083 }
8184 }
8285
@@ -94,7 +97,7 @@ protected void onPostExecute(SearchResults results) {
9497
9598 if (results .markerOptions == null ) {
9699 if (ConnectivityUtility .isOnline (context )) {
97- Toast .makeText (context , "Unknown location " , Toast .LENGTH_LONG ).show ();
100+ Toast .makeText (context , "Could not find address. " , Toast .LENGTH_LONG ).show ();
98101 } else {
99102 Toast .makeText (context , "No connectivity, try again later." , Toast .LENGTH_LONG ).show ();
100103 }
@@ -104,12 +107,16 @@ protected void onPostExecute(SearchResults results) {
104107 m .showInfoWindow ();
105108 }
106109
107- map .animateCamera (CameraUpdateFactory .newLatLngBounds (results .bounds , 10 ));
110+ CameraPosition position = CameraPosition .builder ()
111+ .target (results .markerOptions .getPosition ())
112+ .zoom (results .zoom ).build ();
113+
114+ map .animateCamera (CameraUpdateFactory .newCameraPosition (position ));
108115 }
109116 }
110117
111118 public class SearchResults {
112119 public MarkerOptions markerOptions ;
113- public LatLngBounds bounds ;
120+ public int zoom ;
114121 }
115122}
0 commit comments