Skip to content

Commit c697a84

Browse files
committed
Añadidos mapas con Mapbox
1 parent 50c764d commit c697a84

33 files changed

+323
-13
lines changed

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ dependencies {
3737
implementation(libs.material)
3838
implementation(libs.activity)
3939
implementation(libs.constraintlayout)
40+
implementation("com.mapbox.maps:android:10.9.1")
4041
implementation("androidx.room:room-runtime:2.8.2")
4142
annotationProcessor("androidx.room:room-compiler:2.8.2")
4243
}

app/src/main/AndroidManifest.xml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:tools="http://schemas.android.com/tools" >
3+
xmlns:tools="http://schemas.android.com/tools">
44

55
<application
66
android:allowBackup="true"
@@ -11,13 +11,16 @@
1111
android:roundIcon="@mipmap/ic_launcher_round"
1212
android:supportsRtl="true"
1313
android:theme="@style/Theme.TODOList"
14-
tools:targetApi="31" >
14+
tools:targetApi="31">
15+
<activity
16+
android:name=".activity.MapActivity"
17+
android:exported="false" />
1518
<activity
1619
android:name=".activity.RegisterTaskActivity"
1720
android:exported="false" />
1821
<activity
1922
android:name=".activity.MainActivity"
20-
android:exported="true" >
23+
android:exported="true">
2124
<intent-filter>
2225
<action android:name="android.intent.action.MAIN" />
2326

app/src/main/java/com/svalero/todolist/activity/MainActivity.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
import android.os.Bundle;
55
import android.view.Menu;
66
import android.view.MenuItem;
7-
import android.view.View;
8-
import android.widget.EditText;
9-
import android.widget.Toast;
107

118
import androidx.annotation.NonNull;
129
import androidx.appcompat.app.AppCompatActivity;
@@ -69,6 +66,11 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
6966
Intent intent = new Intent(this, RegisterTaskActivity.class);
7067
startActivity(intent);
7168

69+
return true;
70+
} else if (item.getItemId() == R.id.tasks_map_item) {
71+
Intent intent = new Intent(this, MapActivity.class);
72+
startActivity(intent);
73+
7274
return true;
7375
}
7476

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.svalero.todolist.activity;
2+
3+
import android.graphics.BitmapFactory;
4+
import android.os.Bundle;
5+
6+
import androidx.annotation.NonNull;
7+
import androidx.appcompat.app.AppCompatActivity;
8+
9+
import com.mapbox.geojson.Point;
10+
import com.mapbox.maps.MapView;
11+
import com.mapbox.maps.Style;
12+
import com.mapbox.maps.plugin.annotation.generated.PointAnnotationManager;
13+
import com.mapbox.maps.plugin.annotation.generated.PointAnnotationOptions;
14+
import com.svalero.todolist.R;
15+
import com.svalero.todolist.db.AppDatabase;
16+
import com.svalero.todolist.db.DatabaseUtil;
17+
import com.svalero.todolist.domain.Task;
18+
import com.svalero.todolist.util.MapUtils;
19+
20+
import java.util.List;
21+
22+
public class MapActivity extends AppCompatActivity implements Style.OnStyleLoaded {
23+
24+
private MapView mapView;
25+
private PointAnnotationManager pointAnnotationManager;
26+
27+
@Override
28+
protected void onCreate(Bundle savedInstanceState) {
29+
super.onCreate(savedInstanceState);
30+
setContentView(R.layout.activity_map);
31+
32+
mapView = findViewById(R.id.mainMap);
33+
mapView.getMapboxMap().loadStyleUri(Style.MAPBOX_STREETS, this);
34+
35+
pointAnnotationManager = MapUtils.buildAnnotationManager(mapView);
36+
}
37+
38+
@Override
39+
public void onStyleLoaded(@NonNull Style style) {
40+
final AppDatabase db = DatabaseUtil.getDb(this);
41+
List<Task> allTasks = db.taskDao().findAll();
42+
showTasksAsMarkers(allTasks);
43+
}
44+
45+
private void showTasksAsMarkers(List<Task> tasks) {
46+
tasks.forEach(task -> {
47+
Point point = Point.fromLngLat(task.getLongitude(), task.getLatitude());
48+
MapUtils.addMarker(this, pointAnnotationManager, point, task.getName());
49+
});
50+
}
51+
}
Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,58 @@
11
package com.svalero.todolist.activity;
22

3+
import android.graphics.BitmapFactory;
34
import android.os.Bundle;
45
import android.view.View;
56
import android.widget.CheckBox;
67
import android.widget.EditText;
78
import android.widget.Toast;
89

9-
import androidx.activity.EdgeToEdge;
10+
import androidx.annotation.NonNull;
1011
import androidx.appcompat.app.AppCompatActivity;
11-
import androidx.core.graphics.Insets;
12-
import androidx.core.view.ViewCompat;
13-
import androidx.core.view.WindowInsetsCompat;
1412

13+
import com.mapbox.geojson.Point;
14+
import com.mapbox.maps.MapView;
15+
import com.mapbox.maps.Style;
16+
import com.mapbox.maps.plugin.annotation.AnnotationConfig;
17+
import com.mapbox.maps.plugin.annotation.AnnotationPlugin;
18+
import com.mapbox.maps.plugin.annotation.AnnotationPluginImplKt;
19+
import com.mapbox.maps.plugin.annotation.generated.PointAnnotationManager;
20+
import com.mapbox.maps.plugin.annotation.generated.PointAnnotationManagerKt;
21+
import com.mapbox.maps.plugin.annotation.generated.PointAnnotationOptions;
22+
import com.mapbox.maps.plugin.gestures.GesturesPlugin;
23+
import com.mapbox.maps.plugin.gestures.GesturesUtils;
24+
import com.mapbox.maps.plugin.gestures.OnMapClickListener;
1525
import com.svalero.todolist.R;
1626
import com.svalero.todolist.db.AppDatabase;
1727
import com.svalero.todolist.db.DatabaseUtil;
1828
import com.svalero.todolist.domain.Task;
29+
import com.svalero.todolist.util.MapUtils;
1930

20-
public class RegisterTaskActivity extends AppCompatActivity {
31+
public class RegisterTaskActivity extends AppCompatActivity implements OnMapClickListener {
32+
33+
private MapView mapView;
34+
private GesturesPlugin gesturesPlugin;
35+
private PointAnnotationManager pointAnnotationManager;
36+
private Point currentPoint;
2137

2238
@Override
2339
protected void onCreate(Bundle savedInstanceState) {
2440
super.onCreate(savedInstanceState);
2541
setContentView(R.layout.activity_register_task);
42+
43+
mapView = findViewById(R.id.mapView);
44+
mapView.getMapboxMap().loadStyleUri(Style.MAPBOX_STREETS);
45+
46+
initializeGesturesPlugin();
47+
pointAnnotationManager = MapUtils.buildAnnotationManager(mapView);
48+
}
49+
50+
@Override
51+
public boolean onMapClick(@NonNull Point point) {
52+
pointAnnotationManager.deleteAll();
53+
currentPoint = point;
54+
MapUtils.addMarker(this, pointAnnotationManager, point);
55+
return false;
2656
}
2757

2858
public void addTask(View view) {
@@ -35,14 +65,20 @@ public void addTask(View view) {
3565
boolean done = cbTaskDone.isChecked();
3666
// TODO Validar entrada
3767

38-
Task task = new Task(name, description, done);
68+
Task task = new Task(name, description, done, currentPoint.latitude(), currentPoint.longitude());
3969
final AppDatabase db = DatabaseUtil.getDb(this);
4070
db.taskDao().insert(task);
4171

4272
Toast.makeText(this, R.string.task_added, Toast.LENGTH_SHORT).show();
4373
etTaskName.setText("");
4474
etTaskDescription.setText("");
4575
cbTaskDone.setChecked(false);
76+
pointAnnotationManager.deleteAll();
4677
etTaskName.requestFocus();
4778
}
79+
80+
private void initializeGesturesPlugin() {
81+
gesturesPlugin = GesturesUtils.getGestures(mapView);
82+
gesturesPlugin.addOnMapClickListener(this);
83+
}
4884
}

app/src/main/java/com/svalero/todolist/domain/Task.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,22 @@ public class Task {
1515
private String description;
1616
@ColumnInfo
1717
private boolean done;
18+
@ColumnInfo
19+
private double latitude;
20+
@ColumnInfo
21+
private double longitude;
1822

1923
@Ignore
2024
public Task(String name) {
2125
this.name = name;
2226
}
2327

24-
public Task(String name, String description, boolean done) {
28+
public Task(String name, String description, boolean done, double latitude, double longitude) {
2529
this.name = name;
2630
this.description = description;
2731
this.done = done;
32+
this.latitude = latitude;
33+
this.longitude = longitude;
2834
}
2935

3036
public int getId() {
@@ -58,4 +64,20 @@ public boolean isDone() {
5864
public void setDone(boolean done) {
5965
this.done = done;
6066
}
67+
68+
public double getLatitude() {
69+
return latitude;
70+
}
71+
72+
public void setLatitude(double latitude) {
73+
this.latitude = latitude;
74+
}
75+
76+
public double getLongitude() {
77+
return longitude;
78+
}
79+
80+
public void setLongitude(double longitude) {
81+
this.longitude = longitude;
82+
}
6183
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.svalero.todolist.util;
2+
3+
import android.content.Context;
4+
import android.graphics.BitmapFactory;
5+
6+
import com.mapbox.geojson.Point;
7+
import com.mapbox.maps.MapView;
8+
import com.mapbox.maps.plugin.annotation.AnnotationConfig;
9+
import com.mapbox.maps.plugin.annotation.AnnotationPlugin;
10+
import com.mapbox.maps.plugin.annotation.AnnotationPluginImplKt;
11+
import com.mapbox.maps.plugin.annotation.generated.PointAnnotationManager;
12+
import com.mapbox.maps.plugin.annotation.generated.PointAnnotationManagerKt;
13+
import com.mapbox.maps.plugin.annotation.generated.PointAnnotationOptions;
14+
import com.svalero.todolist.R;
15+
16+
public class MapUtils {
17+
18+
public static PointAnnotationManager buildAnnotationManager(MapView mapView) {
19+
AnnotationPlugin annotationPlugin = AnnotationPluginImplKt.getAnnotations(mapView);
20+
AnnotationConfig annotationConfig = new AnnotationConfig();
21+
return PointAnnotationManagerKt.createPointAnnotationManager(
22+
annotationPlugin, annotationConfig);
23+
}
24+
25+
public static void addMarker(Context context, PointAnnotationManager pointAnnotationManager,
26+
Point point, String message) {
27+
PointAnnotationOptions annotationOptions = new PointAnnotationOptions()
28+
.withPoint(point)
29+
.withIconImage(BitmapFactory.decodeResource(context.getResources(), R.mipmap.red_marker_foreground))
30+
.withTextField(message);
31+
pointAnnotationManager.create(annotationOptions);
32+
}
33+
34+
public static void addMarker(Context context, PointAnnotationManager pointAnnotationManager, Point point) {
35+
addMarker(context, pointAnnotationManager, point, context.getResources().getString(R.string.here));
36+
}
37+
}
37 KB
Loading
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:width="108dp"
4+
android:height="108dp"
5+
android:viewportWidth="108"
6+
android:viewportHeight="108">
7+
<group android:scaleX="1.01"
8+
android:scaleY="1.01"
9+
android:translateX="-0.54"
10+
android:translateY="-0.54">
11+
<path android:fillColor="#3DDC84"
12+
android:pathData="M0,0h108v108h-108z"/>
13+
<path android:fillColor="#00000000" android:pathData="M9,0L9,108"
14+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
15+
<path android:fillColor="#00000000" android:pathData="M19,0L19,108"
16+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
17+
<path android:fillColor="#00000000" android:pathData="M29,0L29,108"
18+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
19+
<path android:fillColor="#00000000" android:pathData="M39,0L39,108"
20+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
21+
<path android:fillColor="#00000000" android:pathData="M49,0L49,108"
22+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
23+
<path android:fillColor="#00000000" android:pathData="M59,0L59,108"
24+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
25+
<path android:fillColor="#00000000" android:pathData="M69,0L69,108"
26+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
27+
<path android:fillColor="#00000000" android:pathData="M79,0L79,108"
28+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
29+
<path android:fillColor="#00000000" android:pathData="M89,0L89,108"
30+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
31+
<path android:fillColor="#00000000" android:pathData="M99,0L99,108"
32+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
33+
<path android:fillColor="#00000000" android:pathData="M0,9L108,9"
34+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
35+
<path android:fillColor="#00000000" android:pathData="M0,19L108,19"
36+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
37+
<path android:fillColor="#00000000" android:pathData="M0,29L108,29"
38+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
39+
<path android:fillColor="#00000000" android:pathData="M0,39L108,39"
40+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
41+
<path android:fillColor="#00000000" android:pathData="M0,49L108,49"
42+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
43+
<path android:fillColor="#00000000" android:pathData="M0,59L108,59"
44+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
45+
<path android:fillColor="#00000000" android:pathData="M0,69L108,69"
46+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
47+
<path android:fillColor="#00000000" android:pathData="M0,79L108,79"
48+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
49+
<path android:fillColor="#00000000" android:pathData="M0,89L108,89"
50+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
51+
<path android:fillColor="#00000000" android:pathData="M0,99L108,99"
52+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
53+
<path android:fillColor="#00000000" android:pathData="M19,29L89,29"
54+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
55+
<path android:fillColor="#00000000" android:pathData="M19,39L89,39"
56+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
57+
<path android:fillColor="#00000000" android:pathData="M19,49L89,49"
58+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
59+
<path android:fillColor="#00000000" android:pathData="M19,59L89,59"
60+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
61+
<path android:fillColor="#00000000" android:pathData="M19,69L89,69"
62+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
63+
<path android:fillColor="#00000000" android:pathData="M19,79L89,79"
64+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
65+
<path android:fillColor="#00000000" android:pathData="M29,19L29,89"
66+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
67+
<path android:fillColor="#00000000" android:pathData="M39,19L39,89"
68+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
69+
<path android:fillColor="#00000000" android:pathData="M49,19L49,89"
70+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
71+
<path android:fillColor="#00000000" android:pathData="M59,19L59,89"
72+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
73+
<path android:fillColor="#00000000" android:pathData="M69,19L69,89"
74+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
75+
<path android:fillColor="#00000000" android:pathData="M79,19L79,89"
76+
android:strokeColor="#33FFFFFF" android:strokeWidth="0.8"/>
77+
</group>
78+
</vector>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:id="@+id/main"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
tools:context=".activity.MapActivity">
9+
10+
<com.mapbox.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android"
11+
xmlns:tools="http://schemas.android.com/tools"
12+
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
13+
android:id="@+id/mainMap"
14+
android:layout_width="match_parent"
15+
android:layout_height="match_parent"
16+
mapbox:mapbox_cameraTargetLat="41.65606"
17+
mapbox:mapbox_cameraTargetLng="-0.87734"
18+
mapbox:mapbox_cameraZoom="9.0"
19+
/>
20+
21+
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)