从您的移动设备获取 GPS 数据，并在地图上绘制您的位置和速度

创建与移动设备的连接

`m = mobiledev;`

准备从位置传感器获取数据

`m.PositionSensorEnabled = 1;`

开始获取数据

`m.Logging = 1;`

停止记录数据

`m.Logging = 0;`

检索记录的位置数据

`[lat,lon,t,spd] = poslog(m);`

`load drivingAroundMathWorks lat lon spd;`

将速度划分为以颜色值表示的 bin

```nBins = 10; binSpacing = (max(spd) - min(spd))/nBins; binRanges = min(spd):binSpacing:max(spd)-binSpacing; % Add an inf to binRanges to enclose the values above the last bin. binRanges(end+1) = inf; % |histc| determines which bin each speed value falls into. [~, spdBins] = histc(spd, binRanges);```

按速度拆分纬度和经度数据

```lat = lat'; lon = lon'; spdBins = spdBins'; % Create a geographical shape vector, which stores the line segments as % features. s = geoshape(); for k = 1:nBins % Keep only the lat/lon values which match the current bin. Leave the % rest as NaN, which are interpreted as breaks in the line segments. latValid = nan(1, length(lat)); latValid(spdBins==k) = lat(spdBins==k); lonValid = nan(1, length(lon)); lonValid(spdBins==k) = lon(spdBins==k); % To make the path continuous despite being segmented into different % colors, the lat/lon values that occur after transitioning from the % current speed bin to another speed bin will need to be kept. transitions = [diff(spdBins) 0]; insertionInd = find(spdBins==k & transitions~=0) + 1; % Preallocate space for and insert the extra lat/lon values. latSeg = zeros(1, length(latValid) + length(insertionInd)); latSeg(insertionInd + (0:length(insertionInd)-1)) = lat(insertionInd); latSeg(~latSeg) = latValid; lonSeg = zeros(1, length(lonValid) + length(insertionInd)); lonSeg(insertionInd + (0:length(insertionInd)-1)) = lon(insertionInd); lonSeg(~lonSeg) = lonValid; % Add the lat/lon segments to the geographic shape vector. s(k) = geoshape(latSeg, lonSeg); end```

创建 Web 地图和路线叠加

Mapping Toolbox 提供了许多处理 Web 地图的函数。

`wm = webmap('Open Street Map');`

```mwLat = 42.299827; mwLon = -71.350273; name = 'MathWorks'; iconDir = fullfile(matlabroot,'toolbox','matlab','icons'); iconFilename = fullfile(iconDir, 'matlabicon.gif'); wmmarker(mwLat, mwLon, 'FeatureName', name, 'Icon', iconFilename);```

`colors = autumn(nBins);`

`wmline(s, 'Color', colors, 'Width', 5);`

`wmzoom(16);`

清理

```m.PositionSensorEnabled = 0; clear m;```