Skip to content

Commit e61b0d2

Browse files
Add files via upload
1 parent 4208442 commit e61b0d2

36 files changed

+894
-0
lines changed

app/build.gradle

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
plugins {
2+
id 'com.android.application'
3+
}
4+
5+
android {
6+
namespace 'com.example.mapbox'
7+
compileSdk 33
8+
9+
defaultConfig {
10+
applicationId "com.example.mapbox"
11+
minSdk 24
12+
targetSdk 33
13+
versionCode 1
14+
versionName "1.0"
15+
16+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
}
18+
19+
buildTypes {
20+
release {
21+
minifyEnabled false
22+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23+
}
24+
}
25+
compileOptions {
26+
sourceCompatibility JavaVersion.VERSION_1_8
27+
targetCompatibility JavaVersion.VERSION_1_8
28+
}
29+
}
30+
31+
dependencies {
32+
33+
implementation 'androidx.appcompat:appcompat:1.6.1'
34+
implementation 'com.google.android.material:material:1.9.0'
35+
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
36+
testImplementation 'junit:junit:4.13.2'
37+
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
38+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
39+
implementation 'com.mapbox.maps:android:10.14.1'
40+
}

app/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.example.mapbox;
2+
3+
import android.content.Context;
4+
5+
import androidx.test.platform.app.InstrumentationRegistry;
6+
import androidx.test.ext.junit.runners.AndroidJUnit4;
7+
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
import static org.junit.Assert.*;
12+
13+
/**
14+
* Instrumented test, which will execute on an Android device.
15+
*
16+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
17+
*/
18+
@RunWith(AndroidJUnit4.class)
19+
public class ExampleInstrumentedTest {
20+
@Test
21+
public void useAppContext() {
22+
// Context of the app under test.
23+
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24+
assertEquals("com.example.mapbox", appContext.getPackageName());
25+
}
26+
}

app/src/main/AndroidManifest.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
tools:ignore="CoarseFineLocation">
5+
6+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
7+
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
8+
9+
<application
10+
android:allowBackup="true"
11+
android:dataExtractionRules="@xml/data_extraction_rules"
12+
android:fullBackupContent="@xml/backup_rules"
13+
android:icon="@mipmap/ic_launcher"
14+
android:label="@string/app_name"
15+
android:roundIcon="@mipmap/ic_launcher_round"
16+
android:supportsRtl="true"
17+
android:theme="@style/Theme.Mapbox"
18+
tools:targetApi="31">
19+
<activity
20+
android:name=".MainActivity"
21+
android:exported="true">
22+
<intent-filter>
23+
<action android:name="android.intent.action.MAIN" />
24+
25+
<category android:name="android.intent.category.LAUNCHER" />
26+
</intent-filter>
27+
</activity>
28+
</application>
29+
30+
</manifest>
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.example.mapbox;
2+
3+
import static com.mapbox.maps.plugin.gestures.GesturesUtils.getGestures;
4+
import static com.mapbox.maps.plugin.locationcomponent.LocationComponentUtils.getLocationComponent;
5+
6+
import android.Manifest;
7+
import android.content.pm.PackageManager;
8+
import android.os.Bundle;
9+
import android.view.View;
10+
import android.widget.Toast;
11+
12+
import androidx.activity.result.ActivityResultCallback;
13+
import androidx.activity.result.ActivityResultLauncher;
14+
import androidx.activity.result.contract.ActivityResultContracts;
15+
import androidx.annotation.NonNull;
16+
import androidx.appcompat.app.AppCompatActivity;
17+
import androidx.appcompat.content.res.AppCompatResources;
18+
import androidx.core.app.ActivityCompat;
19+
20+
import com.google.android.material.floatingactionbutton.FloatingActionButton;
21+
import com.mapbox.android.gestures.MoveGestureDetector;
22+
import com.mapbox.geojson.Point;
23+
import com.mapbox.maps.CameraOptions;
24+
import com.mapbox.maps.MapView;
25+
import com.mapbox.maps.Style;
26+
import com.mapbox.maps.plugin.LocationPuck2D;
27+
import com.mapbox.maps.plugin.gestures.OnMoveListener;
28+
import com.mapbox.maps.plugin.locationcomponent.LocationComponentPlugin;
29+
import com.mapbox.maps.plugin.locationcomponent.OnIndicatorBearingChangedListener;
30+
import com.mapbox.maps.plugin.locationcomponent.OnIndicatorPositionChangedListener;
31+
32+
public class MainActivity extends AppCompatActivity {
33+
private MapView mapView;
34+
FloatingActionButton floatingActionButton;
35+
36+
private final ActivityResultLauncher<String> activityResultLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(), new ActivityResultCallback<Boolean>() {
37+
@Override
38+
public void onActivityResult(Boolean result) {
39+
if (result) {
40+
Toast.makeText(MainActivity.this, "Permission granted!", Toast.LENGTH_SHORT).show();
41+
}
42+
}
43+
});
44+
45+
private final OnIndicatorBearingChangedListener onIndicatorBearingChangedListener = new OnIndicatorBearingChangedListener() {
46+
@Override
47+
public void onIndicatorBearingChanged(double v) {
48+
mapView.getMapboxMap().setCamera(new CameraOptions.Builder().bearing(v).build());
49+
}
50+
};
51+
52+
private final OnIndicatorPositionChangedListener onIndicatorPositionChangedListener = new OnIndicatorPositionChangedListener() {
53+
@Override
54+
public void onIndicatorPositionChanged(@NonNull Point point) {
55+
mapView.getMapboxMap().setCamera(new CameraOptions.Builder().center(point).zoom(20.0).build());
56+
getGestures(mapView).setFocalPoint(mapView.getMapboxMap().pixelForCoordinate(point));
57+
}
58+
};
59+
60+
private final OnMoveListener onMoveListener = new OnMoveListener() {
61+
@Override
62+
public void onMoveBegin(@NonNull MoveGestureDetector moveGestureDetector) {
63+
getLocationComponent(mapView).removeOnIndicatorBearingChangedListener(onIndicatorBearingChangedListener);
64+
getLocationComponent(mapView).removeOnIndicatorPositionChangedListener(onIndicatorPositionChangedListener);
65+
getGestures(mapView).removeOnMoveListener(onMoveListener);
66+
floatingActionButton.show();
67+
}
68+
69+
@Override
70+
public boolean onMove(@NonNull MoveGestureDetector moveGestureDetector) {
71+
return false;
72+
}
73+
74+
@Override
75+
public void onMoveEnd(@NonNull MoveGestureDetector moveGestureDetector) {
76+
77+
}
78+
};
79+
80+
@Override
81+
protected void onCreate(Bundle savedInstanceState) {
82+
super.onCreate(savedInstanceState);
83+
setContentView(R.layout.activity_main);
84+
85+
mapView = findViewById(R.id.mapView);
86+
floatingActionButton = findViewById(R.id.focusLocation);
87+
88+
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
89+
activityResultLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION);
90+
}
91+
92+
floatingActionButton.hide();
93+
mapView.getMapboxMap().loadStyleUri(Style.SATELLITE, new Style.OnStyleLoaded() {
94+
@Override
95+
public void onStyleLoaded(@NonNull Style style) {
96+
mapView.getMapboxMap().setCamera(new CameraOptions.Builder().zoom(20.0).build());
97+
LocationComponentPlugin locationComponentPlugin = getLocationComponent(mapView);
98+
locationComponentPlugin.setEnabled(true);
99+
LocationPuck2D locationPuck2D = new LocationPuck2D();
100+
locationPuck2D.setBearingImage(AppCompatResources.getDrawable(MainActivity.this, R.drawable.baseline_location_on_24));
101+
locationComponentPlugin.setLocationPuck(locationPuck2D);
102+
locationComponentPlugin.addOnIndicatorBearingChangedListener(onIndicatorBearingChangedListener);
103+
locationComponentPlugin.addOnIndicatorPositionChangedListener(onIndicatorPositionChangedListener);
104+
getGestures(mapView).addOnMoveListener(onMoveListener);
105+
106+
floatingActionButton.setOnClickListener(new View.OnClickListener() {
107+
@Override
108+
public void onClick(View view) {
109+
locationComponentPlugin.addOnIndicatorBearingChangedListener(onIndicatorBearingChangedListener);
110+
locationComponentPlugin.addOnIndicatorPositionChangedListener(onIndicatorPositionChangedListener);
111+
getGestures(mapView).addOnMoveListener(onMoveListener);
112+
floatingActionButton.hide();
113+
}
114+
});
115+
}
116+
});
117+
}
118+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector android:height="24dp" android:tint="#000000"
2+
android:viewportHeight="24" android:viewportWidth="24"
3+
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
4+
<path android:fillColor="@android:color/white" android:pathData="M12,2C8.13,2 5,5.13 5,9c0,5.25 7,13 7,13s7,-7.75 7,-13c0,-3.87 -3.13,-7 -7,-7zM12,11.5c-1.38,0 -2.5,-1.12 -2.5,-2.5s1.12,-2.5 2.5,-2.5 2.5,1.12 2.5,2.5 -1.12,2.5 -2.5,2.5z"/>
5+
</vector>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector android:height="24dp" android:tint="#000000"
2+
android:viewportHeight="24" android:viewportWidth="24"
3+
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
4+
<path android:fillColor="@android:color/white" android:pathData="M12,8c-2.21,0 -4,1.79 -4,4s1.79,4 4,4 4,-1.79 4,-4 -1.79,-4 -4,-4zM20.94,11c-0.46,-4.17 -3.77,-7.48 -7.94,-7.94L13,1h-2v2.06C6.83,3.52 3.52,6.83 3.06,11L1,11v2h2.06c0.46,4.17 3.77,7.48 7.94,7.94L11,23h2v-2.06c4.17,-0.46 7.48,-3.77 7.94,-7.94L23,13v-2h-2.06zM12,19c-3.87,0 -7,-3.13 -7,-7s3.13,-7 7,-7 7,3.13 7,7 -3.13,7 -7,7z"/>
5+
</vector>

0 commit comments

Comments
 (0)