Geocoding
Contents
The Mobile Maps SDK examples require that you first complete the initial project set-up.
Geocode an address from a search query and zoom to that location on the map.
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
Before running the Java or Kotlin code, the theme needs to be set in the Theme.xml
file as shown below.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.AsyncAcctInit" parent="Theme.AppCompat" />
</resources>
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.trimblemaps.account.LicensedFeature;
import com.trimblemaps.account.TrimbleMapsAccountManager;
import com.trimblemaps.account.models.TrimbleMapsAccount;
import com.trimblemaps.api.geocoding.v1.TrimbleMapsGeocoding;
import com.trimblemaps.api.geocoding.v1.models.GeocodingResponse;
import com.trimblemaps.api.geocoding.v1.models.TrimbleMapsLocation;
import com.trimblemaps.api.models.v1.models.Coords;
import com.trimblemaps.mapsdk.TrimbleMaps;
import com.trimblemaps.mapsdk.camera.CameraPosition;
import com.trimblemaps.mapsdk.camera.CameraUpdateFactory;
import com.trimblemaps.mapsdk.geometry.LatLng;
import com.trimblemaps.mapsdk.maps.MapView;
import com.trimblemaps.mapsdk.maps.OnMapReadyCallback;
import com.trimblemaps.mapsdk.maps.Style;
import com.trimblemaps.mapsdk.maps.TrimbleMapsMap;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
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("Your-API-key-here")
.addLicensedFeature(LicensedFeature.MAPS_SDK)
.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
}
});
}
/**
* Activity Overrides
*/
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
public void onStart() {
super.onStart();
mapView.onStart();
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
public void onStop() {
super.onStop();
mapView.onStop();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
}
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.trimblemaps.account.LicensedFeature
import com.trimblemaps.account.TrimbleMapsAccountManager
import com.trimblemaps.account.models.TrimbleMapsAccount
import com.trimblemaps.api.geocoding.v1.TrimbleMapsGeocoding
import com.trimblemaps.api.geocoding.v1.models.GeocodingResponse
import com.trimblemaps.api.geocoding.v1.models.TrimbleMapsLocation
import com.trimblemaps.api.models.v1.models.Coords
import com.trimblemaps.mapsdk.TrimbleMaps
import com.trimblemaps.mapsdk.camera.CameraPosition
import com.trimblemaps.mapsdk.camera.CameraUpdateFactory
import com.trimblemaps.mapsdk.geometry.LatLng
import com.trimblemaps.mapsdk.maps.MapView
import com.trimblemaps.mapsdk.maps.Style
import com.trimblemaps.mapsdk.maps.TrimbleMapsMap
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
class SampleGeocodingActivity : Activity() {
private var mapView: MapView? = null
private var map: TrimbleMapsMap? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Authorize the API key for the session.
// .apiKey() requires your Trimble Maps API key
val trimbleMapsAccount = TrimbleMapsAccount.builder()
.apiKey("Your-API-key-here")
.addLicensedFeature(LicensedFeature.MAPS_SDK)
.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 = findViewById<View>(R.id.mapView) as MapView
// the onMapReadyCallback is fired when the map is ready to be worked with
mapView!!.getMapAsync { trimbleMapsMap ->
// The TrimbleMapsMap object is created, now a style can be applied to render a map.
map = trimbleMapsMap
map!!.setStyle(Style.MOBILE_DEFAULT) { // The style is loaded, you can add content to the map, move it etc.
// Style was loaded, do a geocode.
geocode()
}
}
}
private fun geocode() {
val geocoding = TrimbleMapsGeocoding.builder()
.query("1 Independence Way Princeton NJ 08540") // The search query to geocode on
.build()
geocoding.enqueueCall(object : Callback<GeocodingResponse?> {
override fun onResponse(call: Call<GeocodingResponse?>, response: Response<GeocodingResponse?>) {
// Get the locations list from the response
val results: List<TrimbleMapsLocation> = response.body()?.locations() as List<TrimbleMapsLocation>
// If there are results available, zoom to that location on the map.
if (results.isNotEmpty()) {
// Get the first result
val firstResult = results[0]
// Pull out the coordinates
val geocodedCoordinates: Coords = firstResult.coords()
// Zoom to that location
val cameraPosition = CameraPosition.Builder()
.target(LatLng(geocodedCoordinates.lat().toDouble(), geocodedCoordinates.lon().toDouble()))
.zoom(13.0)
.build()
map!!.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition))
}
}
override fun onFailure(call: Call<GeocodingResponse?>, t: Throwable) {
// Geocoding failed
}
})
}
/**
* Activity Overrides
*/
override fun onStart() {
super.onStart()
mapView?.onStart()
}
override fun onResume() {
super.onResume()
mapView?.onResume()
}
override fun onPause() {
super.onPause()
mapView?.onPause()
}
override fun onStop() {
super.onStop()
mapView?.onStop()
}
override fun onLowMemory() {
super.onLowMemory()
mapView?.onLowMemory()
}
override fun onDestroy() {
super.onDestroy()
mapView?.onDestroy()
}
}