Skip to main content

Dots on a map

Contents

Visualize data on top of the map. Data could vary from simple dots-on-a-map (the example below) to more complex pieces like 3D buildings, route paths, geofences, and more.

dots on a map

activity_sample_dots_on_a_map.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>

SampleDotsOnAMapActivity.java

public class SampleDotsOnAMapActivity extends AppCompatActivity implements Style.OnStyleLoaded {

    private MapView mapView;
    private TrimbleMapsMap map;
    private final String SOURCE_ID = "tristatepoints";
    private final String LAYER_ID = "tristatepoints";

    @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))
                .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_dots_on_a_map);

        // 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;
                CameraPosition position = new CameraPosition.Builder()
                        .target(new LatLng(41.36290180612575, -74.6946761628674))
                        .zoom(13)
                        .build();
                map.setCameraPosition(position);

                // This class implements the onStyleLoaded method, that will be called when
                // the style has been loaded.
                map.setStyle(Style.MOBILE_NIGHT, SampleDotsOnAMapActivity.this);
            }
        });
    }


    @Override
    public void onStyleLoaded(Style style) {
        // In this example a .json file from the assets folder is being used as the source
        // This sample can be found here: @TODO ADD LINK
        // Geojson can either be passed in as a string or the result of a URI call.

        try {
            // Create a source and add it to the style. Important to note, sources are linked to styles.
            // If you change the style you may need to re-add your source and layers
            style.addSource(new GeoJsonSource(SOURCE_ID, new URI("asset://tristate.json")));
            // See sample JSON file (tristate.json) below

            // Create a CircleLayer to display our source information.
            CircleLayer circleLayer = new CircleLayer(LAYER_ID, SOURCE_ID);
            circleLayer.setProperties(
                    PropertyFactory.circleRadius(4f),
                    PropertyFactory.circleColor(Color.WHITE),
                    PropertyFactory.circleStrokeColor(Color.RED),
                    PropertyFactory.circleStrokeWidth(5f)

            );

            // add the layer
            style.addLayer(circleLayer);

        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

    }
}

Sample JSON (tristate.json)

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -74.68954,
          41.369863
        ]
      },
      "properties": {
        "state": "NY"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -74.687882,
          41.36892
        ]
      },
      "properties": {
        "state": "NY"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -74.687007,
          41.349811
        ]
      },
      "properties": {
        "state": "NJ"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -74.68222,
          41.36056
        ]
      },
      "properties": {
        "state": "NY"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -74.704292,
          41.364475
        ]
      },
      "properties": {
        "state": "PA"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -74.710335,
          41.360504
        ]
      },
      "properties": {
        "state": "PA"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -74.695556,
          41.367119
        ]
      },
      "properties": {
        "state": "PA"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -74.694232,
          41.354974
        ]
      },
      "properties": {
        "state": "NJ"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -74.698967,
          41.370361
        ]
      },
      "properties": {
        "state": "PA"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          -74.709572,
          41.360474
        ]
      },
      "properties": {
        "state": "PA"
      }
    }
  ]
}
Last updated March 16, 2022.
Contents