Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] Add Grid source sample for Custom Geometry Source
Browse files Browse the repository at this point in the history
  • Loading branch information
Asheem Mamoowala committed Nov 20, 2017
1 parent 0179a28 commit b73fe5d
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,17 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>

<activity
android:name=".activity.style.GridSourceActivity"
android:label="@string/title_activity_grid_source"
android:description="@string/description_grid_source">
<meta-data
android:name="@string/category"
android:value="@string/category_style"/>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>
<!-- Features -->
<activity
android:name=".activity.feature.QueryRenderedFeaturesPropertiesActivity"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package com.mapbox.mapboxsdk.testapp.activity.style;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;

import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.style.layers.LineLayer;
import com.mapbox.mapboxsdk.style.sources.CustomGeometrySource;
import com.mapbox.mapboxsdk.style.sources.GeometryTileProvider;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.services.commons.geojson.Feature;
import com.mapbox.services.commons.geojson.FeatureCollection;
import com.mapbox.services.commons.geojson.MultiLineString;
import com.mapbox.services.commons.models.Position;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineColor;

/**
* Test activity showcasing using CustomGeometrySource to create a grid overlay on the map.
*/
public class GridSourceActivity extends AppCompatActivity implements OnMapReadyCallback {

private static final String ID_GRID_SOURCE = "grid_source";
private static final String ID_GRID_LAYER = "grid_layer";

private MapView mapView;
private MapboxMap mapboxMap;

/**
* Implementation of GeometryTileProvider that returns features representing a zoom-dependent
* grid.
*/
static class GridProvider implements GeometryTileProvider {
public FeatureCollection getFeaturesForBounds(LatLngBounds bounds, int zoom) {
List<Feature> features = new ArrayList<>();
double gridSpacing;
if (zoom >= 13) {
gridSpacing = 0.01;
} else if (zoom >= 11) {
gridSpacing = 0.05;
} else if (zoom == 10) {
gridSpacing = .1;
} else if (zoom == 9) {
gridSpacing = 0.25;
} else if (zoom == 8) {
gridSpacing = 0.5;
} else if (zoom >= 6) {
gridSpacing = 1;
} else if (zoom == 5) {
gridSpacing = 2;
} else if (zoom >= 4) {
gridSpacing = 5;
} else if (zoom == 2) {
gridSpacing = 10;
} else {
gridSpacing = 20;
}

List gridLines = new ArrayList();
for (double y = Math.ceil(bounds.getLatNorth() / gridSpacing) * gridSpacing;
y >= Math.floor(bounds.getLatSouth() / gridSpacing) * gridSpacing; y -= gridSpacing) {
gridLines.add(Arrays.asList(Position.fromCoordinates(bounds.getLonWest(), y),
Position.fromCoordinates(bounds.getLonEast(), y)));
}
features.add(Feature.fromGeometry(MultiLineString.fromCoordinates(gridLines)));

gridLines = new ArrayList();
for (double x = Math.floor(bounds.getLonWest() / gridSpacing) * gridSpacing;
x <= Math.ceil(bounds.getLonEast() / gridSpacing) * gridSpacing; x += gridSpacing) {
gridLines.add(Arrays.asList(Position.fromCoordinates(x, bounds.getLatSouth()),
Position.fromCoordinates(x, bounds.getLatNorth())));
}
features.add(Feature.fromGeometry(MultiLineString.fromCoordinates(gridLines)));

return FeatureCollection.fromFeatures(features);
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid_source);

mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}

@Override
public void onMapReady(@NonNull final MapboxMap map) {
mapboxMap = map;

// add source
CustomGeometrySource source = new CustomGeometrySource(ID_GRID_SOURCE, new GridProvider());
mapboxMap.addSource(source);

// add layer
LineLayer layer = new LineLayer(ID_GRID_LAYER, ID_GRID_SOURCE);
layer.setProperties(
lineColor(Color.parseColor("#000000"))
);

mapboxMap.addLayer(layer);
}

@Override
protected void onStart() {
super.onStart();
mapView.onStart();
}

@Override
public void onResume() {
super.onResume();
mapView.onResume();
}

@Override
public void onPause() {
super.onPause();
mapView.onPause();
}

@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}

@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.mapbox.mapboxsdk.maps.MapView
android:id="@id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:mapbox_cameraTargetLat="41.9567"
app:mapbox_cameraTargetLng="-78.6430"
app:mapbox_cameraZoom="5"
app:mapbox_styleUrl="@string/mapbox_style_mapbox_streets"/>

</RelativeLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<string name="description_query_rendered_feature_properties_point">Query rendered feature properties on click</string>
<string name="description_query_rendered_features_box_count">Count all rendered features in box</string>
<string name="description_query_rendered_features_box_symbol_count">Count all rendered symbols in box</string>
<string name="description_query_rendered_features_box_highlight">Hightligh buildings in box</string>
<string name="description_query_rendered_features_box_highlight">Highlight buildings in box</string>
<string name="description_query_source_features">Query source for features</string>
<string name="description_simple_map">Shows a simple map</string>
<string name="description_map_change">Logs map change events to Logcat</string>
Expand All @@ -67,4 +67,5 @@
<string name="description_textureview_debug">Use TextureView to render the map</string>
<string name="description_textureview_resize">Resize a map rendered on a TextureView</string>
<string name="description_textureview_animate">Animate a map rendered on a TextureView</string>
<string name="description_grid_source">Example Custom Geometry Source</string>
</resources>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Mapbox Android SDK TestApp</string>
<string name="title_activity_grid_source">Grid Source</string>
</resources>

0 comments on commit b73fe5d

Please sign in to comment.