@@ -38,20 +38,62 @@ Then add markers to the map with GeoJSON:
38383 . In ` JsonApiActivity ` we add a new variable for ` MapLibreMap ` .
3939 It is used to add annotations to the map instance.
4040 ``` kotlin
41- -- 8 < -- " MapLibreAndroidTestApp/src/main/java/org/maplibre/android/testapp/activity/annotation/JsonApiActivity.kt:top"
41+ class JsonApiActivity : AppCompatActivity () {
42+
43+ // Declare a variable for MapView
44+ private lateinit var mapView: MapView
45+
46+ // Declare a variable for MapLibreMap
47+ private lateinit var maplibreMap: MapLibreMap }
4248 ```
4349
44504 . Call ` mapview.getMapSync() ` in order to get a ` MapLibreMap ` object.
4551 After ` maplibreMap ` is assigned, call the ` getEarthQuakeDataFromUSGS() ` method
4652 to make a HTTP request and transform data into the map annotations.
4753 ``` kotlin
48- -- 8 < -- " MapLibreAndroidTestApp/src/main/java/org/maplibre/android/testapp/activity/annotation/JsonApiActivity.kt:mapAsync"
54+ mapView.getMapAsync { map ->
55+ maplibreMap = map
56+
57+ maplibreMap.setStyle(" https://demotiles.maplibre.org/style.json" )
58+
59+ // Fetch data from USGS
60+ getEarthQuakeDataFromUSGS()
61+ }
4962 ```
5063
51645 . Define a function ` getEarthQuakeDataFromUSGS() ` to fetch GeoJSON data from a public API.
5265 If we successfully get the response, call ` addMarkersToMap() ` on the UI thread.
5366 ``` kotlin
54- -- 8 < -- " MapLibreAndroidTestApp/src/main/java/org/maplibre/android/testapp/activity/annotation/JsonApiActivity.kt:getEarthquakes"
67+ // Get Earthquake data from usgs.gov, read API doc at:
68+ // https://earthquake.usgs.gov/fdsnws/event/1/
69+ private fun getEarthQuakeDataFromUSGS () {
70+ val url = " https://earthquake.usgs.gov/fdsnws/event/1/query" .toHttpUrl().newBuilder()
71+ .addQueryParameter(" format" , " geojson" )
72+ .addQueryParameter(" starttime" , " 2022-01-01" )
73+ .addQueryParameter(" endtime" , " 2022-12-31" )
74+ .addQueryParameter(" minmagnitude" , " 5.8" )
75+ .addQueryParameter(" latitude" , " 24" )
76+ .addQueryParameter(" longitude" , " 121" )
77+ .addQueryParameter(" maxradius" , " 1.5" )
78+ .build()
79+ val request: Request = Request .Builder ().url(url).build()
80+
81+ OkHttpClient ().newCall(request).enqueue(object : Callback {
82+ override fun onFailure (call : Call , e : IOException ) {
83+ Toast .makeText(this @JsonApiActivity, " Fail to fetch data" , Toast .LENGTH_SHORT )
84+ .show()
85+ }
86+
87+ override fun onResponse (call : Call , response : Response ) {
88+ val featureCollection = response.body?.string()
89+ ?.let (FeatureCollection ::fromJson)
90+ ? : return
91+ // If FeatureCollection in response is not null
92+ // Then add markers to map
93+ runOnUiThread { addMarkersToMap(featureCollection) }
94+ }
95+ })
96+ }
5597 ```
5698
57996 . Now it is time to add markers into the map.
@@ -60,7 +102,58 @@ Then add markers to the map with GeoJSON:
60102 - If the magnitude of an earthquake is bigger than 6.0, we use the red icon. Otherwise, we use the blue one.
61103 - Finally, move the camera to the bounds of the newly added markers
62104 ``` kotlin
63- -- 8 < -- " MapLibreAndroidTestApp/src/main/java/org/maplibre/android/testapp/activity/annotation/JsonApiActivity.kt:addMarkers"
105+ private fun addMarkersToMap (data : FeatureCollection ) {
106+ val bounds = mutableListOf<LatLng >()
107+
108+ // Get bitmaps for marker icon
109+ val infoIconDrawable = ResourcesCompat .getDrawable(
110+ this .resources,
111+ // Intentionally specify package name
112+ // This makes copy from another project easier
113+ org.maplibre.android.R .drawable.maplibre_info_icon_default,
114+ theme
115+ )!!
116+ val bitmapBlue = infoIconDrawable.toBitmap()
117+ val bitmapRed = infoIconDrawable
118+ .mutate()
119+ .apply { setTint(Color .RED ) }
120+ .toBitmap()
121+
122+ // Add symbol for each point feature
123+ data.features()?.forEach { feature ->
124+ val geometry = feature.geometry()?.toJson() ? : return @forEach
125+ val point = Point .fromJson(geometry) ? : return @forEach
126+ val latLng = LatLng (point.latitude(), point.longitude())
127+ bounds.add(latLng)
128+
129+ // Contents in InfoWindow of each marker
130+ val title = feature.getStringProperty(" title" )
131+ val epochTime = feature.getNumberProperty(" time" )
132+ val dateString = SimpleDateFormat (" yyyy/MM/dd HH:mm" , Locale .TAIWAN ).format(epochTime)
133+
134+ // If magnitude > 6.0, show marker with red icon. If not, show blue icon instead
135+ val mag = feature.getNumberProperty(" mag" )
136+ val icon = IconFactory .getInstance(this )
137+ .fromBitmap(if (mag.toFloat() > 6.0 ) bitmapRed else bitmapBlue)
138+
139+ // Use MarkerOptions and addMarker() to add a new marker in map
140+ val markerOptions = MarkerOptions ()
141+ .position(latLng)
142+ .title(dateString)
143+ .snippet(title)
144+ .icon(icon)
145+ maplibreMap.addMarker(markerOptions)
146+ }
147+
148+ // Move camera to newly added annotations
149+ maplibreMap.getCameraForLatLngBounds(LatLngBounds .fromLatLngs(bounds))?.let {
150+ val newCameraPosition = CameraPosition .Builder ()
151+ .target(it.target)
152+ .zoom(it.zoom - 0.5 )
153+ .build()
154+ maplibreMap.cameraPosition = newCameraPosition
155+ }
156+ }
64157 ```
65158
66159[ // ] : # ( 7. Here is the final result. For the full contents of `JsonApiActivity`, please visit source code of our [Test App]. )
@@ -83,4 +176,4 @@ Then add markers to the map with GeoJSON:
83176[ mvn ] : https://mvnrepository.com/artifact/org.maplibre.gl/android-plugin-annotation-v9
84177[ Android Developer Documentation ] : https://developer.android.com/topic/libraries/architecture/coroutines
85178[ MarkerOptions ] : https://maplibre.org/maplibre-native/android/api/-map-libre%20-native%20-android/org.maplibre.android.annotations/-marker-options/index.html
86- [ Test App ] : https://github.com/maplibre/maplibre -native/tree/main/platform/android/MapLibreAndroidTestApp/src/main/java/org/maplibre/android/testapp/activity/annotation/JsonApiActivity.kt
179+ [ Test App ] : https://github.com/MapMetrics/mapmetrics -native-sdk /tree/main/platform/android/MapLibreAndroidTestApp/src/main/java/org/maplibre/android/testapp/activity/annotation/JsonApiActivity.kt
0 commit comments