@@ -69,159 +69,159 @@ To obtain the incidents for a specific feature, you can use the code below. When
69
69
70
70
1. First, you need to edit **res > layout > activity_main.xml**, so that it looks like the one below. You may replace the `mapcontrol_centerLat`, `mapcontrol_centerLng`, and `mapcontrol_zoom` with your desired values. Recall, the zoom level is a value between 0 and 22. At zoom level 0, the entire world fits on a single tile.
71
71
72
- ```XML
73
- <?xml version="1.0" encoding="utf-8"?>
74
- <FrameLayout
75
- xmlns:android="http://schemas.android.com/apk/res/android"
76
- xmlns:app="http://schemas.android.com/apk/res-auto"
77
- android:layout_width="match_parent"
78
- android:layout_height="match_parent"
79
- >
72
+ ```XML
73
+ <?xml version="1.0" encoding="utf-8"?>
74
+ <FrameLayout
75
+ xmlns:android="http://schemas.android.com/apk/res/android"
76
+ xmlns:app="http://schemas.android.com/apk/res-auto"
77
+ android:layout_width="match_parent"
78
+ android:layout_height="match_parent"
79
+ >
80
80
81
- <com.microsoft.azure.maps.mapcontrol.MapControl
82
- android:id="@+id/mapcontrol"
83
- android:layout_width="match_parent"
84
- android:layout_height="match_parent"
85
- app:mapcontrol_centerLat="47.6050"
86
- app:mapcontrol_centerLng="-122.3344"
87
- app:mapcontrol_zoom="12"
88
- />
89
-
90
- </FrameLayout>
91
- ```
81
+ <com.microsoft.azure.maps.mapcontrol.MapControl
82
+ android:id="@+id/mapcontrol"
83
+ android:layout_width="match_parent"
84
+ android:layout_height="match_parent"
85
+ app:mapcontrol_centerLat="47.6050"
86
+ app:mapcontrol_centerLng="-122.3344"
87
+ app:mapcontrol_zoom="12"
88
+ />
89
+
90
+ </FrameLayout>
91
+ ```
92
92
93
93
2. Add the following code to your **MainActivity.java** file. The package is included by default, so make sure you keep your package at the top.
94
94
95
- ```java
96
- package <yourpackagename>;
97
- import androidx.appcompat.app.AppCompatActivity;
98
-
99
- import android.os.Bundle;
100
- import android.widget.Toast;
101
-
102
- import com.microsoft.azure.maps.mapcontrol.AzureMaps;
103
- import com.microsoft.azure.maps.mapcontrol.MapControl;
104
- import com.mapbox.geojson.Feature;
105
- import com.microsoft.azure.maps.mapcontrol.events.OnFeatureClick;
106
-
107
- import com.microsoft.azure.maps.mapcontrol.options.TrafficFlow;
108
- import static com.microsoft.azure.maps.mapcontrol.options.TrafficOptions.flow;
109
- import static com.microsoft.azure.maps.mapcontrol.options.TrafficOptions.incidents;
110
-
111
- public class MainActivity extends AppCompatActivity {
112
-
113
- static {
114
- AzureMaps.setSubscriptionKey("Your Azure Maps Subscription Key");
115
- }
116
-
117
- MapControl mapControl;
118
-
119
- @Override
120
- protected void onCreate(Bundle savedInstanceState) {
121
- super.onCreate(savedInstanceState);
122
- setContentView(R.layout.activity_main);
123
-
124
- mapControl = findViewById(R.id.mapcontrol);
125
-
126
- mapControl.onCreate(savedInstanceState);
127
-
128
- //Wait until the map resources are ready.
129
- mapControl.getMapAsync(map -> {
130
-
131
- map.setTraffic(flow(TrafficFlow.RELATIVE));
132
- map.setTraffic(incidents(true));
133
-
134
- map.events.add((OnFeatureClick) (features) -> {
135
-
136
- if (features != null && features.size() > 0) {
137
- Feature incident = features.get(0);
138
- if (incident.properties() != null) {
139
-
140
-
141
- StringBuilder sb = new StringBuilder();
142
- String incidentType = incident.getStringProperty("incidentType");
143
- if (incidentType != null) {
144
- sb.append(incidentType);
145
- }
146
- if (sb.length() > 0) sb.append("\n ");
147
- if ("Road Closed".equals(incidentType)) {
148
- sb.append(incident.getStringProperty("from"));
149
- } else {
150
- String description = incident.getStringProperty("description");
151
- if (description != null) {
152
- for (String word : description.split(" ")) {
153
- if (word.length() > 0) {
154
- sb.append(word.substring(0, 1).toUpperCase());
155
- if (word.length() > 1) {
156
- sb.append(word.substring(1));
157
- }
158
- sb.append(" ");
159
- }
160
- }
161
- }
162
- }
163
- String message = sb.toString();
164
-
165
- if (message.length() > 0) {
166
- Toast.makeText(this,message,Toast.LENGTH_LONG).show();
167
- }
168
- }
169
- }
170
- });
171
- });
172
- }
173
-
174
- @Override
175
- public void onResume() {
176
- super.onResume();
177
- mapControl.onResume();
178
- }
179
-
180
- @Override
181
- protected void onStart(){
182
- super.onStart();
183
- mapControl.onStart();
184
- }
185
-
186
- @Override
187
- public void onPause() {
188
- super.onPause();
189
- mapControl.onPause();
190
- }
191
-
192
- @Override
193
- public void onStop() {
194
- super.onStop();
195
- mapControl.onStop();
196
- }
197
-
198
- @Override
199
- public void onLowMemory() {
200
- super.onLowMemory();
201
- mapControl.onLowMemory();
202
- }
203
-
204
- @Override
205
- protected void onDestroy() {
206
- super.onDestroy();
207
- mapControl.onDestroy();
208
- }
209
-
210
- @Override
211
- protected void onSaveInstanceState(Bundle outState) {
212
- super.onSaveInstanceState(outState);
213
- mapControl.onSaveInstanceState(outState);
214
- }
215
- }
216
- ```
95
+ ```java
96
+ package <yourpackagename>;
97
+ import androidx.appcompat.app.AppCompatActivity;
98
+
99
+ import android.os.Bundle;
100
+ import android.widget.Toast;
101
+
102
+ import com.microsoft.azure.maps.mapcontrol.AzureMaps;
103
+ import com.microsoft.azure.maps.mapcontrol.MapControl;
104
+ import com.mapbox.geojson.Feature;
105
+ import com.microsoft.azure.maps.mapcontrol.events.OnFeatureClick;
106
+
107
+ import com.microsoft.azure.maps.mapcontrol.options.TrafficFlow;
108
+ import static com.microsoft.azure.maps.mapcontrol.options.TrafficOptions.flow;
109
+ import static com.microsoft.azure.maps.mapcontrol.options.TrafficOptions.incidents;
110
+
111
+ public class MainActivity extends AppCompatActivity {
112
+
113
+ static {
114
+ AzureMaps.setSubscriptionKey("Your Azure Maps Subscription Key");
115
+ }
116
+
117
+ MapControl mapControl;
118
+
119
+ @Override
120
+ protected void onCreate(Bundle savedInstanceState) {
121
+ super.onCreate(savedInstanceState);
122
+ setContentView(R.layout.activity_main);
123
+
124
+ mapControl = findViewById(R.id.mapcontrol);
125
+
126
+ mapControl.onCreate(savedInstanceState);
127
+
128
+ //Wait until the map resources are ready.
129
+ mapControl.getMapAsync(map -> {
130
+
131
+ map.setTraffic(flow(TrafficFlow.RELATIVE));
132
+ map.setTraffic(incidents(true));
133
+
134
+ map.events.add((OnFeatureClick) (features) -> {
135
+
136
+ if (features != null && features.size() > 0) {
137
+ Feature incident = features.get(0);
138
+ if (incident.properties() != null) {
139
+
140
+
141
+ StringBuilder sb = new StringBuilder();
142
+ String incidentType = incident.getStringProperty("incidentType");
143
+ if (incidentType != null) {
144
+ sb.append(incidentType);
145
+ }
146
+ if (sb.length() > 0) sb.append("\n ");
147
+ if ("Road Closed".equals(incidentType)) {
148
+ sb.append(incident.getStringProperty("from"));
149
+ } else {
150
+ String description = incident.getStringProperty("description");
151
+ if (description != null) {
152
+ for (String word : description.split(" ")) {
153
+ if (word.length() > 0) {
154
+ sb.append(word.substring(0, 1).toUpperCase());
155
+ if (word.length() > 1) {
156
+ sb.append(word.substring(1));
157
+ }
158
+ sb.append(" ");
159
+ }
160
+ }
161
+ }
162
+ }
163
+ String message = sb.toString();
164
+
165
+ if (message.length() > 0) {
166
+ Toast.makeText(this,message,Toast.LENGTH_LONG).show();
167
+ }
168
+ }
169
+ }
170
+ });
171
+ });
172
+ }
173
+
174
+ @Override
175
+ public void onResume() {
176
+ super.onResume();
177
+ mapControl.onResume();
178
+ }
179
+
180
+ @Override
181
+ protected void onStart(){
182
+ super.onStart();
183
+ mapControl.onStart();
184
+ }
185
+
186
+ @Override
187
+ public void onPause() {
188
+ super.onPause();
189
+ mapControl.onPause();
190
+ }
191
+
192
+ @Override
193
+ public void onStop() {
194
+ super.onStop();
195
+ mapControl.onStop();
196
+ }
197
+
198
+ @Override
199
+ public void onLowMemory() {
200
+ super.onLowMemory();
201
+ mapControl.onLowMemory();
202
+ }
203
+
204
+ @Override
205
+ protected void onDestroy() {
206
+ super.onDestroy();
207
+ mapControl.onDestroy();
208
+ }
209
+
210
+ @Override
211
+ protected void onSaveInstanceState(Bundle outState) {
212
+ super.onSaveInstanceState(outState);
213
+ mapControl.onSaveInstanceState(outState);
214
+ }
215
+ }
216
+ ```
217
217
218
218
3. Once you incorporate the above code in your application, you' ll be able to click on a feature and see the details of the traffic incidents. Depending on the latitude, longitude, and the zoom level values that you used in your ** activity_main. xml** file, you' ll see results similar to the following image:
219
219
220
- <center>
220
+ <center>
221
221
222
- 
222
+ 
223
223
224
- </center>
224
+ </center>
225
225
226
226
## Next steps
227
227
@@ -237,4 +237,4 @@ View the following guides to learn how to add more data to your map:
237
237
> [Add shapes to android map](how-to-add-shapes-to-android-map.md)
238
238
239
239
> [!div class="nextstepaction"]
240
- > [Display feature information](display-feature-information-android.md)
240
+ > [Display feature information](display-feature-information-android.md)
0 commit comments