// Apple Map ウィジェットを返す
return AppleMap(
initialCameraPosition: const CameraPosition(
target: LatLng(35.6809591, 139.7673068),
zoom: 14.5,
),
myLocationEnabled: true,
onMapCreated: _onMapCreated,
);
targetのところを具体的な緯度経度から'_yourLocation.latitude,_yourLocation.longitude'に変更すると以下のエラーが発生します。
色々ググりましたが、解決策が見つからず質問させて頂きました。
: Error: Not a constant expression.
lib/main.dart:90
target: LatLng(_yourLocation.latitude, _yourLocation.longitude),
^^^^^^^^^^^^^
: Error: Not a constant expression.
lib/main.dart:90
target: LatLng(_yourLocation.latitude, _yourLocation.longitude),
^^^^^^^^^^^^^
: Error: Property 'latitude' cannot be accessed on 'LocationData?' because it is potentially null.
- 'LocationData' is from 'package:location_platform_interface/location_platform_interface.dart' ('../flutter/.pub-cache/hosted/pub.dartlang.org/location_platform_interface-2.3.0/lib/location_platform_interface.dart').
package:location_platform_interface/location_platform_interface.dart:1
Try accessing using ?. instead.
target: LatLng(_yourLocation.latitude, _yourLocation.longitude),
^^^^^^^^
: Context: '_yourLocation' refers to a property so it couldn't be promoted.
lib/main.dart:38
See http://dart.dev/go/non-promo-property
LocationData? _yourLocation;
- 'LocationData' is from 'package:location_platform_interface/location_platform_interface.dart' ('../flutter/.pub-cache/hosted/pub.dartlang.org/location_platform_interface-2.3.0/lib/location_platform_interface.dart').
package:location_platform_interface/location_platform_interface.dart:1
Try accessing using ?. instead.
target: LatLng(_yourLocation.latitude, _yourLocation.longitude),
^^^^^^^^
: Context: '_yourLocation' refers to a property so it couldn't be promoted.
lib/main.dart:38
See http://dart.dev/go/non-promo-property
LocationData? _yourLocation;
^
: Error: Property 'longitude' cannot be accessed on 'LocationData?' because it is potentially null.
.
.
.
: Error: The argument type 'double?' can't be assigned to the parameter type 'double' because 'double?' is nullable and 'double' isn't.
lib/main.dart:90
target: LatLng(_yourLocation.latitude, _yourLocation.longitude),
main.dart
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:apple_maps_flutter/apple_maps_flutter.dart';
import 'package:location/location.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Map',
home: AppleMaps(),
);
}
}
class AppleMaps extends StatefulWidget {
const AppleMaps({Key? key}) : super(key: key);
@override
_AppleMapsState createState() => _AppleMapsState();
}
class _AppleMapsState extends State<AppleMaps> {
late AppleMapController mapController;
void _onMapCreated(AppleMapController controller) {
mapController = controller;
}
final Location _locationService = Location();
// 現在位置
LocationData? _yourLocation;
// 現在位置の監視状況
StreamSubscription? _locationChangedListen;
void _getLocation() async {
LocationData location = await _locationService.getLocation();
setState(() {
_yourLocation = location;
});
}
@override
void initState() {
super.initState();
// 現在位置の取得
_getLocation();
// 現在位置の変化を監視
_locationChangedListen =
_locationService.onLocationChanged.listen((LocationData result) async {
setState(() {
_yourLocation = result;
});
});
}
@override
void dispose() {
super.dispose();
// 監視を終了
_locationChangedListen?.cancel();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: _makeAppleMap(),
);
}
Widget _makeAppleMap() {
if (_yourLocation == null) {
// 現在位置が取れるまではローディング中
return const Center(
child: CircularProgressIndicator(),
);
} else {
// Apple Map ウィジェットを返す
return AppleMap(
initialCameraPosition: const CameraPosition(
target: LatLng(_yourLocation.latitude,_yourLocation.longitude),
zoom: 14.5,
),
myLocationEnabled: true,
onMapCreated: _onMapCreated,
);
}
}
}