getBingUrlTemplate function
Returns the URL template in the required format for the Bing Maps.
For Bing Maps, an additional step is required. The format of the required URL varies from the other tile services. Hence, we have added this top-level function which returns the URL in the required format.
You can use the URL template returned from this function to set it to the MapTileLayer.urlTemplate property.
MapZoomPanBehavior _zoomPanBehavior;
@override
void initState() {
_zoomPanBehavior = MapZoomPanBehavior();
super.initState();
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: getBingUrlTemplate(
'http://dev.virtualearth.net/REST/V1/Imagery/Metadata/Road
OnDemand?output=json&include=ImageryProviders&key=YOUR_KEY'),
builder: (context, snapshot) {
if (snapshot.hasData) {
return SfMaps(
layers: [
MapTileLayer(
initialFocalLatLng: MapLatLng(20.5937, 78.9629),
zoomPanBehavior: _zoomPanBehavior,
initialZoomLevel: 3,
urlTemplate: snapshot.data,
),
],
);
}
return CircularProgressIndicator();
});
}
Implementation
Future<String?> getBingUrlTemplate(String url) async {
final http.Response response = await _fetchResponse(url);
assert(response.statusCode == 200, 'Invalid key');
if (response.statusCode == 200) {
final Map<String, dynamic> decodedJson =
// ignore: avoid_as
json.decode(response.body) as Map<String, dynamic>;
late String imageUrl;
late String imageUrlSubDomains;
if (decodedJson['authenticationResultCode'] == 'ValidCredentials') {
for (final String key in decodedJson.keys) {
if (key == 'resourceSets') {
// ignore: avoid_as
final List<dynamic> resourceSets = decodedJson[key] as List<dynamic>;
for (final dynamic key in resourceSets[0].keys) {
if (key == 'resources') {
final List<dynamic> resources =
// ignore: avoid_as
resourceSets[0][key] as List<dynamic>;
final Map<String, dynamic> resourcesMap =
// ignore: avoid_as
resources[0] as Map<String, dynamic>;
imageUrl = resourcesMap['imageUrl'].toString();
final List<dynamic> subDomains =
// ignore: avoid_as
resourcesMap['imageUrlSubdomains'] as List<dynamic>;
imageUrlSubDomains = subDomains[0].toString();
break;
}
}
break;
}
}
final List<String> splitUrl = imageUrl.split('{subdomain}');
return splitUrl[0] + imageUrlSubDomains + splitUrl[1];
}
}
return null;
}