Created
July 1, 2019 14:06
-
-
Save sajjadyousefnia/828478147a21ba0d0ff5ee89563596dc to your computer and use it in GitHub Desktop.
MapBoxSample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sajjady.simplegis; | |
import androidx.annotation.NonNull; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.annotation.SuppressLint; | |
import android.graphics.BitmapFactory; | |
import android.graphics.Color; | |
import android.graphics.PointF; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.util.Log; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.view.ViewOverlay; | |
import android.widget.Button; | |
import com.mapbox.android.gestures.AndroidGesturesManager; | |
import com.mapbox.android.gestures.BaseGesture; | |
import com.mapbox.android.gestures.ShoveGestureDetector; | |
import com.mapbox.android.gestures.StandardGestureDetector; | |
import com.mapbox.geojson.Feature; | |
import com.mapbox.geojson.FeatureCollection; | |
import com.mapbox.geojson.LineString; | |
import com.mapbox.geojson.Point; | |
import com.mapbox.mapboxsdk.Mapbox; | |
import com.mapbox.mapboxsdk.geometry.LatLng; | |
import com.mapbox.mapboxsdk.maps.MapView; | |
import com.mapbox.mapboxsdk.maps.MapboxMap; | |
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; | |
import com.mapbox.mapboxsdk.maps.Projection; | |
import com.mapbox.mapboxsdk.maps.Style; | |
import com.mapbox.mapboxsdk.plugins.annotation.LineManager; | |
import com.mapbox.mapboxsdk.plugins.annotation.SymbolManager; | |
import com.mapbox.mapboxsdk.plugins.annotation.SymbolOptions; | |
import com.mapbox.mapboxsdk.style.layers.LineLayer; | |
import com.mapbox.mapboxsdk.style.layers.Property; | |
import com.mapbox.mapboxsdk.style.layers.PropertyFactory; | |
import com.mapbox.mapboxsdk.style.layers.SymbolLayer; | |
import com.mapbox.mapboxsdk.style.layers.TransitionOptions; | |
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; | |
import java.util.ArrayList; | |
public class MainActivity extends AppCompatActivity { | |
private MapboxMap mapboxMap; | |
private static final String TAG = "MainActivity"; | |
private MapView mapView; | |
private Button buttonMove; | |
private Boolean mapIsMotionLess = false; | |
private LineManager lineManager; | |
private Handler handler = new Handler(); | |
private Style style; | |
private AndroidGesturesManager gesturesManager; | |
private LatLng animatedPosition; | |
private Boolean screenIsLockedFor = false; | |
private GeoJsonSource geoJsonSource; | |
private Runnable runnableCode = new Runnable() { | |
@Override | |
public void run() { | |
if (gesturesManager.getDetectors().get(0).getCurrentEvent().getAction() != MotionEvent.ACTION_UP) { | |
handler.postDelayed(this, 100); | |
} else { | |
screenIsLockedFor = false; | |
removeLayerAndSource(style, "linelayer", "line-source"); | |
handler.removeCallbacks(this); | |
} | |
} | |
}; | |
// Create the Handler object (on the main thread by default) | |
@SuppressLint("ClickableViewAccessibility") | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
Mapbox.getInstance(this, "sk.eyJ1Ijoic2R5dXNzdWYiLCJhIjoiY2p4ZmxoOWoyMDhteTNubDFnMnJzZjJmZSJ9.Ac-TKeogqZR7iNzChWSFxA"); | |
setContentView(R.layout.activity_main); | |
buttonMove = findViewById(R.id.lock_move); | |
mapView = findViewById(R.id.mapView); | |
mapView.onCreate(savedInstanceState); | |
mapView.getMapAsync(new OnMapReadyCallback() { | |
@Override | |
public void onMapReady(@NonNull MapboxMap mapboxMap) { | |
MainActivity.this.mapboxMap = mapboxMap; | |
mapboxMap.setStyle(Style.LIGHT, new Style.OnStyleLoaded() { | |
@SuppressLint("ClickableViewAccessibility") | |
@Override | |
public void onStyleLoaded(@NonNull Style style) { | |
MainActivity.this.style = style; | |
style.setTransition(new TransitionOptions(0, 0, true)); | |
style.addImage("marker-icon-id", | |
BitmapFactory.decodeResource( | |
MainActivity.this.getResources(), R.drawable.mapbox_marker_icon_default)); | |
GeoJsonSource geoJsonSource = new GeoJsonSource("source-id", Feature.fromGeometry( | |
Point.fromLngLat(-87.679, 41.885))); | |
style.addSource(geoJsonSource); | |
SymbolLayer symbolLayer = new SymbolLayer("layer-id", "source-id"); | |
symbolLayer.withProperties( | |
PropertyFactory.iconImage("marker-icon-id") | |
); | |
style.addLayer(symbolLayer); | |
//------ | |
// second marker | |
style.addImage("marker-icon-id2", | |
BitmapFactory.decodeResource( | |
MainActivity.this.getResources(), R.drawable.mapbox_marker_icon_default)); | |
GeoJsonSource geoJsonSource2 = new GeoJsonSource("source-id2", Feature.fromGeometry( | |
Point.fromLngLat(36.2605, 59.6168))); | |
style.addSource(geoJsonSource2); | |
SymbolLayer symbolLayer2 = new SymbolLayer("layer-id2", "source-id2"); | |
symbolLayer2.withProperties( | |
PropertyFactory.iconImage("marker-icon-id2") | |
); | |
style.addLayer(symbolLayer2); | |
lineManager = new LineManager(mapView, mapboxMap, style); | |
buttonMove.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
changeMapMotionStatus(); | |
} | |
}); | |
AndroidGesturesManager gesturesManager = mapboxMap.getGesturesManager(); | |
MainActivity.this.gesturesManager = gesturesManager; | |
gesturesManager.setStandardGestureListener(new StandardGestureDetector.StandardOnGestureListener() { | |
@Override | |
public boolean onSingleTapConfirmed(MotionEvent motionEvent) { | |
return false; | |
} | |
@Override | |
public boolean onDoubleTap(MotionEvent motionEvent) { | |
return false; | |
} | |
@Override | |
public boolean onDoubleTapEvent(MotionEvent motionEvent) { | |
return false; | |
} | |
@Override | |
public boolean onDown(MotionEvent motionEvent) { | |
if (mapIsMotionLess) { | |
if (!screenIsLockedFor) { | |
handler.post(runnableCode); | |
} | |
screenIsLockedFor = true; | |
matchLine(motionEvent, style); | |
} | |
return true; | |
} | |
@Override | |
public void onShowPress(MotionEvent motionEvent) { | |
} | |
@Override | |
public boolean onSingleTapUp(MotionEvent motionEvent) { | |
return false; | |
} | |
@Override | |
public boolean onScroll(MotionEvent firstEvent, MotionEvent secondEvent, float distanceX, float distanceY) { | |
if (mapIsMotionLess) { | |
if (!screenIsLockedFor) { | |
handler.post(runnableCode); | |
} | |
screenIsLockedFor = true; | |
matchLine(secondEvent, style); | |
Log.e(TAG, "onScroll: second" + secondEvent.getAction()); | |
} | |
return true; | |
} | |
@Override | |
public void onLongPress(MotionEvent motionEvent) { | |
removeLayerAndSource(style, "linelayer", "line-source"); | |
} | |
@Override | |
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) { | |
return false; | |
} | |
}); | |
} | |
}); | |
} | |
}); | |
} | |
private void changeMapMotionStatus() { | |
mapboxMap.getUiSettings().setZoomGesturesEnabled(mapIsMotionLess); | |
mapboxMap.getUiSettings().setScrollGesturesEnabled(mapIsMotionLess); | |
mapIsMotionLess = !mapIsMotionLess; | |
} | |
// Add the mapView's own lifecycle methods to the activity's lifecycle methods | |
@Override | |
public void onStart() { | |
super.onStart(); | |
mapView.onStart(); | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
mapView.onResume(); | |
} | |
@Override | |
public void onPause() { | |
super.onPause(); | |
mapView.onPause(); | |
} | |
@Override | |
public void onStop() { | |
super.onStop(); | |
mapView.onStop(); | |
} | |
@Override | |
public void onLowMemory() { | |
super.onLowMemory(); | |
mapView.onLowMemory(); | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
mapView.onDestroy(); | |
} | |
@Override | |
protected void onSaveInstanceState(Bundle outState) { | |
super.onSaveInstanceState(outState); | |
mapView.onSaveInstanceState(outState); | |
} | |
private void matchLine(MotionEvent motionEvent, @NonNull Style style) { | |
LatLng endPointLatLng = motionEventPointToLatLng(new PointF(), motionEvent); | |
ArrayList<Point> routeCoordinates = getCoordinates(endPointLatLng); | |
if (style.getSource("line-source") == null) { | |
drawLine(style, routeCoordinates); | |
} else { | |
geoJsonSource.setGeoJson(FeatureCollection.fromFeatures(new Feature[]{Feature.fromGeometry( | |
LineString.fromLngLats(routeCoordinates) | |
)})); | |
/*removeLayerAndSource(style, "linelayer", "line-source"); | |
drawLine(style, routeCoordinates);*/ | |
} | |
} | |
private LatLng motionEventPointToLatLng(PointF pointF, MotionEvent motionEvent) { | |
pointF.x = motionEvent.getX(); | |
pointF.y = motionEvent.getY(); | |
Projection projection = mapboxMap.getProjection(); | |
return projection.fromScreenLocation(pointF); | |
} | |
private void removeLayerAndSource(Style style, String layerName, String sourceName) { | |
style.removeLayer(layerName); | |
style.removeSource(sourceName); | |
} | |
private ArrayList<Point> getCoordinates(LatLng endPoint) { | |
ArrayList<Point> routeCoordinates = new ArrayList<>(); | |
routeCoordinates.add(Point.fromLngLat(endPoint.getLongitude(), endPoint.getLatitude())); | |
routeCoordinates.add(Point.fromLngLat(-87.679, 41.885)); | |
return routeCoordinates; | |
} | |
private void drawLine(Style style, ArrayList<Point> routeCoordinates) { | |
style.addLayer(new LineLayer("linelayer", "line-source").withProperties( | |
PropertyFactory.lineDasharray(new Float[]{0.01f, 2f}), | |
PropertyFactory.lineCap(Property.LINE_CAP_ROUND), | |
PropertyFactory.lineJoin(Property.LINE_JOIN_ROUND), | |
PropertyFactory.lineWidth(5f), | |
PropertyFactory.lineColor(Color.parseColor("#e55e5e")) | |
)); | |
geoJsonSource = new GeoJsonSource("line-source", | |
FeatureCollection.fromFeatures(new Feature[]{Feature.fromGeometry( | |
LineString.fromLngLats(routeCoordinates) | |
)})); | |
style.addSource(geoJsonSource); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment