如何在颤振中使用HERE地图获取用户的当前位置?



我使用的是HERE地图flutter的最新SDK版本,即4.6.0.6(探索版(。我想在地图上显示用户的当前位置。请帮忙,因为我在官方文件中找不到它。

对于flutter,我建议您使用地理定位器来获取用户的当前位置

示例代码:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:geolocator/geolocator.dart';
class DashboardScreen extends StatefulWidget {
@override
_DashboardState createState() => _DashboardState();
}
class _DashboardState extends State<DashboardScreen> {
final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;
Position _currentPosition;
String _currentAddress;
@override
void initState() {
super.initState();
_getCurrentLocation();
}
_getCurrentLocation() {
geolocator
.getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
.then((Position position) {
setState(() {
_currentPosition = position;
});
_getAddressFromLatLng();
}).catchError((e) {
print(e);
});
}
_getAddressFromLatLng() async {
try {
List<Placemark> p = await geolocator.placemarkFromCoordinates(
_currentPosition.latitude, _currentPosition.longitude);
Placemark place = p[0];
setState(() {
_currentAddress =
"${place.locality}, ${place.postalCode}, ${place.country}";
});
} catch (e) {
print(e);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("DASHBOARD"),
),
body: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
decoration: BoxDecoration(
color: Theme.of(context).canvasColor,
),
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Icon(Icons.location_on),
SizedBox(
width: 8,
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Location',
style: Theme.of(context).textTheme.caption,
),
if (_currentPosition != null &&
_currentAddress != null)
Text(_currentAddress,
style:
Theme.of(context).textTheme.bodyText2),
],
),
),
SizedBox(
width: 8,
),
],
),
],
))
],
),
),
);
}
}

最新更新