Skip to content

Commit 85ccc81

Browse files
author
farah-alyasari
committed
added values to xml file, resolved review comments, and ran the code one last time to ensure it works as expected
1 parent c60b4fa commit 85ccc81

File tree

1 file changed

+51
-48
lines changed

1 file changed

+51
-48
lines changed

articles/azure-maps/how-to-show-traffic-android.md

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,58 @@ protected void onCreate(Bundle savedInstanceState) {
3737
}
3838
```
3939

40+
## Flow traffic data
41+
42+
You'll first need to import the following libraries to call `setTraffic` and `flow`:
43+
44+
```java
45+
import com.microsoft.azure.maps.mapcontrol.options.TrafficFlow;
46+
import static com.microsoft.azure.maps.mapcontrol.options.TrafficOptions.flow;
47+
```
48+
49+
Use the following code snippet to set traffic flow data. Similar to the code in the previous section, we pass the return value of the `flow` method to the `setTraffic` method. There are four values that can be passed to `flow`, and each value would trigger `flow` to return the respective value. The return value of `flow` will then be passed as the argument to `setTraffic`. See the table below for these four values:
50+
51+
| | |
52+
| :-- | :-- |
53+
| TrafficFlow.NONE | Doesn't display traffic data on the map |
54+
| TrafficFlow.RELATIVE | Shows traffic data that's relative to the free-flow speed of the road |
55+
| TrafficFlow.RELATIVE_DELAY | Displays areas that are slower than the average expected delay |
56+
| TrafficFlow.ABSOLUTE | Shows the absolute speed of all vehicles on the road |
57+
58+
```java
59+
protected void onCreate(Bundle savedInstanceState) {
60+
super.onCreate(savedInstanceState);
61+
mapControl.getMapAsync(map ->
62+
map.setTraffic(flow(TrafficFlow.RELATIVE)));
63+
}
64+
```
65+
4066
## Show incident traffic data by clicking a feature
4167
4268
To obtain the incidents for a specific feature, you can use the code below. When a feature is clicked, the code logic checks for incidents and builds a message about the incident. A message shows up at the bottom of the screen with the details.
4369
44-
1. First, you need to edit **res > layout > activity_main.xml**, so that it looks like the one below. Replace the placeholders for `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.
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.
4571
46-
```XML
47-
<?xml version="1.0" encoding="utf-8"?>
48-
<FrameLayout
49-
xmlns:android="http://schemas.android.com/apk/res/android"
50-
xmlns:app="http://schemas.android.com/apk/res-auto"
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+
81+
<com.microsoft.azure.maps.mapcontrol.MapControl
82+
android:id="@+id/mapcontrol"
5183
android:layout_width="match_parent"
5284
android:layout_height="match_parent"
53-
>
54-
55-
<com.microsoft.azure.maps.mapcontrol.MapControl
56-
android:id="@+id/mapcontrol"
57-
android:layout_width="match_parent"
58-
android:layout_height="match_parent"
59-
app:mapcontrol_centerLat="<choose a latitude>"
60-
app:mapcontrol_centerLng="<choose a latitude>"
61-
app:mapcontrol_zoom="<choose a zoom level>"
62-
/>
63-
64-
</FrameLayout>
65-
```
85+
app:mapcontrol_centerLat="47.6050"
86+
app:mapcontrol_centerLng="-122.3344"
87+
app:mapcontrol_zoom="12"
88+
/>
89+
90+
</FrameLayout>
91+
```
6692
6793
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.
6894
@@ -73,18 +99,19 @@ import androidx.appcompat.app.AppCompatActivity;
7399
import android.os.Bundle;
74100
import android.widget.Toast;
75101
76-
//import android.support.v7.app.AppCompatActivity;
77102
import com.microsoft.azure.maps.mapcontrol.AzureMaps;
78103
import com.microsoft.azure.maps.mapcontrol.MapControl;
79104
import com.mapbox.geojson.Feature;
80105
import com.microsoft.azure.maps.mapcontrol.events.OnFeatureClick;
81106
107+
import com.microsoft.azure.maps.mapcontrol.options.TrafficFlow;
108+
import static com.microsoft.azure.maps.mapcontrol.options.TrafficOptions.flow;
82109
import static com.microsoft.azure.maps.mapcontrol.options.TrafficOptions.incidents;
83110
84111
public class MainActivity extends AppCompatActivity {
85112
86113
static {
87-
AzureMaps.setSubscriptionKey("TiZ3a_OjcwOE471HGCzoPGa-6996WZQKUEuWu_BLRBg");
114+
AzureMaps.setSubscriptionKey("Your Azure Maps Subscription Key");
88115
}
89116
90117
MapControl mapControl;
@@ -100,6 +127,8 @@ public class MainActivity extends AppCompatActivity {
100127
101128
//Wait until the map resources are ready.
102129
mapControl.getMapAsync(map -> {
130+
131+
map.setTraffic(flow(TrafficFlow.RELATIVE));
103132
map.setTraffic(incidents(true));
104133
105134
map.events.add((OnFeatureClick) (features) -> {
@@ -186,40 +215,14 @@ public class MainActivity extends AppCompatActivity {
186215
}
187216
```
188217
189-
3. Once you incorporate the above code in your application, you'll be able to click on a feature and learn about the traffic incidents for that feature. Depending on the latitude, longitude, and the zoom level values that you selected in your **activity_main.xml** file, you'll see results similar to the following image:
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:
190219
191220
<center>
192221
193222
![Incident-traffic-on-the-map](./media/how-to-show-traffic-android/android-traffic.png)
194223
195224
</center>
196225
197-
## Flow traffic data
198-
199-
You'll first need to import the following libraries to call `setTraffic` and `flow`:
200-
201-
```java
202-
import com.microsoft.azure.maps.mapcontrol.options.TrafficFlow;
203-
import static com.microsoft.com.azure.maps.mapcontrol.options.TrafficOptions.flow;
204-
```
205-
206-
Use the following code snippet to set traffic flow data. Similar to the code in the previous section, we pass the return value of the `flow` method to the `setTraffic` method. There are four values that can be passed to `flow`, and each value would trigger `flow` to return the respective value. The return value of `flow` will then be passed as the argument to `setTraffic`. See the table below for these four values:
207-
208-
| | |
209-
| :-- | :-- |
210-
| TrafficFlow.NONE | Doesn't display traffic data on the map |
211-
| TrafficFlow.RELATIVE | Shows traffic data that's relative to the free-flow speed of the road |
212-
| TrafficFlow.RELATIVE_DELAY | Displays areas that are slower than the average expected delay |
213-
| TrafficFlow.ABSOLUTE | Shows the absolute speed of all vehicles on the road |
214-
215-
```java
216-
protected void onCreate(Bundle savedInstanceState) {
217-
super.onCreate(savedInstanceState);
218-
mapControl.getMapAsync(map ->
219-
map.setTraffic(flow(TrafficFlow.RELATIVE)));
220-
}
221-
```
222-
223226
## Next steps
224227
225228
View the following guides to learn how to add more data to your map:

0 commit comments

Comments
 (0)