Skip to main content

Lines on a map

Contents

Draw lines on a map using LineString objects.

lines on a map

activity_sample_lines_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>

SampleLinesOnAMapActivity.java

public class SampleLinesOnAMapActivity extends AppCompatActivity  implements Style.OnStyleLoaded {

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

    @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_lines_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(40.60902838712187, -97.73800045737227))
                        .zoom(2.5)
                        .build();
                map.setCameraPosition(position);

                // This class implements the onStyleLoaded method, that will be called when
                // the style has been loaded.
                map.setStyle(Style.MOBILE_DAY, SampleLinesOnAMapActivity.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://lines.json")));
            // // See sample JSON file (lines.json) below

            // Create a LineLayer to display our source information.
            LineLayer lineLayer = new LineLayer(LAYER_ID, SOURCE_ID);
            lineLayer.setProperties(
                    PropertyFactory.lineWidth(6f),
                    PropertyFactory.lineColor(Color.BLUE),
                    PropertyFactory.lineOpacity(.8f)

            );

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

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

    }
}

Sample JSON (lines.json)

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            -119.091796875,
            44.08758502824516
          ],
          [
            -108.80859375,
            44.213709909702054
          ],
          [
            -114.2578125,
            44.213709909702054
          ],
          [
            -113.90625,
            36.31512514748051
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            -111.09374999999999,
            36.94989178681327
          ],
          [
            -111.357421875,
            42.5530802889558
          ],
          [
            -107.138671875,
            41.83682786072714
          ],
          [
            -108.10546875,
            39.36827914916014
          ],
          [
            -111.005859375,
            39.30029918615029
          ],
          [
            -108.017578125,
            36.80928470205937
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            -105.64453124999999,
            42.22851735620852
          ],
          [
            -100.1953125,
            42.35854391749705
          ],
          [
            -102.919921875,
            42.22851735620852
          ],
          [
            -103.0078125,
            36.94989178681327
          ],
          [
            -105.908203125,
            37.020098201368114
          ],
          [
            -99.931640625,
            36.94989178681327
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            -98.349609375,
            36.87962060502676
          ],
          [
            -98.173828125,
            42.48830197960227
          ],
          [
            -95.80078125,
            39.33429742980725
          ],
          [
            -94.0869140625,
            42.5530802889558
          ],
          [
            -94.04296874999999,
            37.055177106660814
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            -92.548828125,
            42.48830197960227
          ],
          [
            -92.548828125,
            36.73888412439431
          ],
          [
            -89.1650390625,
            37.26530995561875
          ],
          [
            -89.4287109375,
            39.50404070558415
          ],
          [
            -92.021484375,
            39.842286020743394
          ],
          [
            -89.384765625,
            40.51379915504413
          ],
          [
            -89.384765625,
            42.391008609205045
          ],
          [
            -92.493896484375,
            42.49640294093705
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            -87.4072265625,
            42.35854391749705
          ],
          [
            -87.9345703125,
            37.23032838760387
          ],
          [
            -83.583984375,
            37.19533058280065
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            -78.837890625,
            37.33522435930639
          ],
          [
            -82.4853515625,
            37.3002752813443
          ],
          [
            -82.44140625,
            39.774769485295465
          ],
          [
            -79.27734374999999,
            39.70718665682654
          ],
          [
            -82.44140625,
            40.01078714046552
          ],
          [
            -82.3974609375,
            41.705728515237524
          ],
          [
            -79.1455078125,
            41.672911819602085
          ]
        ]
      }
    }
  ]
}
Last updated September 5, 2022.
Contents