Geocoding

Contents

Geocode an address from a search query and zoom to that location on the map.

geocoding

activity_sample_geocoding.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent">

    <com.trimblemaps.mapsdk.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

SampleGeocodingActivity.java

public class SampleGeocodingActivity extends AppCompatActivity {
    private MapView mapView;
    private TrimbleMapsMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Authorize the api key for the session.
        // .apiKey() requires your Trimble Maps API key
        TrimbleMapsAccount trimbleMapsAccount = TrimbleMapsAccount.builder()
                .apiKey(getString(R.string.API_KEY))
                .build();

        // Initialize the session
        TrimbleMapsAccountManager.initialize(trimbleMapsAccount);
        TrimbleMapsAccountManager.awaitInitialization();

        // Get an instance of the map, done before the layout is set.
        TrimbleMaps.getInstance(this);

        setContentView(R.layout.activity_sample_geocoding);

        // Set up the MapView from the layout
        mapView = (MapView) findViewById(R.id.mapView);
        // the onMapReadyCallback is fired when the map is ready to be worked with
        mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(@NonNull TrimbleMapsMap trimbleMapsMap) {
                // The TrimbleMapsMap object is created, now a style can be applied to render a map.
                map = trimbleMapsMap;
                trimbleMapsMap.setStyle(Style.MOBILE_DEFAULT, new Style.OnStyleLoaded() {
                    @Override
                    public void onStyleLoaded(@NonNull Style style) {
                        // The style is loaded, you can add content to the map, move it etc.
                        // Style was loaded, do a geocode.
                        geocode();
                    }
                });
            }
        });
    }

    private void geocode() {
        TrimbleMapsGeocoding geocoding = TrimbleMapsGeocoding.builder()
                .query("1 Independence Way Princeton NJ 08540") // The search query to geocode on
                .build();


        geocoding.enqueueCall(new Callback<GeocodingResponse>() {
            @Override
            public void onResponse(Call<GeocodingResponse> call, Response<GeocodingResponse> response) {
                // Get the locations list from the response
                List<TrimbleMapsLocation> results =  response.body().locations();
                // If there are results available, zoom to that location on the map.
                if(results.size() > 0) {
                    // Get the first result
                    TrimbleMapsLocation firstResult = results.get(0);
                    // Pull out the coordinates
                    Coords geocodedCoordinates = firstResult.coords();
                    // Zoom to that location
                    CameraPosition cameraPosition = new CameraPosition.Builder()
                            .target(new LatLng(Double.parseDouble(geocodedCoordinates.lat()), Double.parseDouble(geocodedCoordinates.lon())))
                            .zoom(13)
                            .build();

                    map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

                }
            }

            @Override
            public void onFailure(Call<GeocodingResponse> call, Throwable t) {
                // Geocoding failed
            }
        });
    }
}
Last updated September 23, 2021.