Highlight Buildings
Contents
The Mobile Maps SDK examples require that you first complete the initial project set-up.
Highlight a 2D building when the user clicks on it.
activity_sample_highlight_building.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" 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>
Sample Code
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>
package com.trimblemaps.navigationexamples;
import android.content.Context;
import android.graphics.PointF;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.trimblemaps.account.LicensedFeature;
import com.trimblemaps.account.TrimbleMapsAccountManager;
import com.trimblemaps.account.models.TrimbleMapsAccount;
import com.trimblemaps.geojson.FeatureCollection;
import com.trimblemaps.mapsdk.TrimbleMaps;
import com.trimblemaps.mapsdk.camera.CameraPosition;
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 com.trimblemaps.mapsdk.style.layers.Layer;
import com.trimblemaps.mapsdk.style.layers.LineLayer;
import com.trimblemaps.mapsdk.style.layers.PropertyFactory;
import com.trimblemaps.mapsdk.style.layers.PropertyValue;
import com.trimblemaps.mapsdk.style.sources.GeoJsonSource;
import com.trimblemaps.mapsdk.style.sources.Source;
import java.util.Collections;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public final class SampleHighlightBuildingActivity extends AppCompatActivity {
private MapView mapView;
private TrimbleMapsMap map;
private final List highlights = Collections.emptyList();
private final String highlightsSrcLayer = "highlighted_buildings";
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TrimbleMapsAccount trimbleMapsAccount = TrimbleMapsAccount.builder().apiKey("Your-API-key-here").addLicensedFeature(LicensedFeature.MAPS_SDK).build();
TrimbleMapsAccountManager.initialize(trimbleMapsAccount);
TrimbleMapsAccountManager.awaitInitialization();
TrimbleMaps.getInstance((Context) this);
setContentView(R.layout.activity_sample_highlight_building);
// Set up the MapView from the layout
mapView = findViewById(R.id.mapView);
if (mapView != null) {
mapView.getMapAsync((OnMapReadyCallback) (new OnMapReadyCallback() {
public final void onMapReady(@NotNull TrimbleMapsMap trimbleMapsMap) {
map = trimbleMapsMap;
map.setStyle((new Style.Builder()).fromUri(Style.MOBILE_NIGHT).withSource((Source) (new GeoJsonSource(highlightsSrcLayer, FeatureCollection.fromFeatures(highlights)))).withLayer((Layer) (new LineLayer(highlightsSrcLayer, highlightsSrcLayer)).withProperties(new PropertyValue[]{PropertyFactory.lineWidth(4.0F), PropertyFactory.lineColor(-256), PropertyFactory.lineOpacity(0.8F)})));
if (map != null) {
map.setCameraPosition((new CameraPosition.Builder()).target(new LatLng(39.96012475826224, -75.16184676002608)).zoom(17.0).build());
map.addOnMapClickListener((TrimbleMapsMap.OnMapClickListener) (new TrimbleMapsMap.OnMapClickListener() {
public final boolean onMapClick(@NotNull LatLng clickedLatLng) {
List<com.trimblemaps.geojson.Feature> features;
PointF pixel = map.getProjection().toScreenLocation(clickedLatLng);
;
if (map != null) {
features = map.queryRenderedFeatures(pixel, new String[]{"building_2d"});
if (map.getStyle() != null) {
GeoJsonSource geoJsonSource = (GeoJsonSource) map.getStyle().getSourceAs(highlightsSrcLayer);
if (geoJsonSource != null) {
geoJsonSource.setGeoJson(FeatureCollection.fromFeatures(features));
}
}
}
return true;
}
}));
}
}
}));
}
}
protected void onStart() {
super.onStart();
if (this.mapView != null) {
this.mapView.onStart();
}
}
protected void onResume() {
super.onResume();
if (this.mapView != null) {
this.mapView.onResume();
}
}
protected void onPause() {
super.onPause();
if (this.mapView != null) {
this.mapView.onPause();
}
}
protected void onStop() {
super.onStop();
if (this.mapView != null) {
this.mapView.onStop();
}
}
public void onLowMemory() {
super.onLowMemory();
if (this.mapView != null) {
this.mapView.onLowMemory();
}
}
protected void onDestroy() {
super.onDestroy();
if (this.mapView != null) {
this.mapView.onDestroy();
}
}
}
package com.trimblemaps.kotlinsamples.samples
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.trimblemaps.account.LicensedFeature
import com.trimblemaps.account.TrimbleMapsAccountManager
import com.trimblemaps.account.models.TrimbleMapsAccount
import com.trimblemaps.geojson.Feature
import com.trimblemaps.geojson.FeatureCollection
import com.trimblemaps.mapsdk.TrimbleMaps
import com.trimblemaps.mapsdk.camera.CameraPosition
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 com.trimblemaps.mapsdk.style.layers.LineLayer
import com.trimblemaps.mapsdk.style.layers.PropertyFactory.lineColor
import com.trimblemaps.mapsdk.style.layers.PropertyFactory.lineOpacity
import com.trimblemaps.mapsdk.style.layers.PropertyFactory.lineWidth
import com.trimblemaps.mapsdk.style.sources.GeoJsonSource
class SampleHighlightBuildingActivity : AppCompatActivity() {
private var mapView: MapView? = null
private var map: TrimbleMapsMap? = null
private var highlights : List<Feature> = listOf()
private var highlightsSrcLayer = "highlighted_buildings"
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_highlight_building)
// Set up the MapView from the layout
mapView = findViewById(R.id.mapView)
// the onMapReadyCallback is fired when the map is ready to be worked with
mapView?.getMapAsync { trimbleMapsMap ->
map = trimbleMapsMap
// The TrimbleMapsMap object is created, now a style can be applied to render a map.
// Adding the source and layer for the buildings highlighted. Building outlines will be
// displayed in yellow
trimbleMapsMap.setStyle(Style
.Builder()
.fromUri(Style.MOBILE_NIGHT)
.withSource(GeoJsonSource(highlightsSrcLayer, FeatureCollection.fromFeatures(highlights)))
.withLayer(LineLayer(highlightsSrcLayer, highlightsSrcLayer).withProperties(
lineWidth(4f),
lineColor(Color.YELLOW),
lineOpacity(.8f)
))
)
map?.cameraPosition = CameraPosition.Builder()
.target(LatLng(39.96012475826224, -75.16184676002608))
.zoom(17.0)
.build()
map?.addOnMapClickListener {clickedLatLng ->
// Convert our LatLng to a pixel
val pixel = map?.projection?.toScreenLocation(clickedLatLng)
// Find any features from the "building_2d" layer that our point intersects
val features = map?.queryRenderedFeatures(pixel!!, "building_2d")
// Update/Replace the source of data with these new found features
map?.style?.getSourceAs<GeoJsonSource>(highlightsSrcLayer)?.setGeoJson(
FeatureCollection.fromFeatures(features!!))
return@addOnMapClickListener true
}
}
}
/**
* 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()
}
}