1

我在一些写着“不需要”的行前添加了一条评论。我添加了这个来标记我认为与问题无关的行。(我没有删除这些行的原因是,如果这些行与问题有关,读者也可以阅读它们。)

class _SafeSheltersPageState extends State<SafeSheltersPage> {
  Future sheltersFuture;     // not needed

  BitmapDescriptor mapMarker1;
  BitmapDescriptor mapMarker2;
  void setCustomMarker(mapMarker, icon) async {
    mapMarker = await BitmapDescriptor.fromAssetImage(
      ImageConfiguration(),
      icon,
    );
  }

  sheltersFutureBuilder() async {
    CurrentLocation currentLocation = await CurrentLocationUtil().getLocation();      // not needed    
    Map<String, Object> data = {     // not needed
      'location': {     // not needed
        'latitude': currentLocation.latitude,     // not needed
        'longitude': currentLocation.longitude,     // not needed
      }     // not needed
    };     // not needed

    return CallApi().postData(data, 'public/api/getShelterLocations');     // not needed
  }     // not needed

  @override
  void initState() {
    super.initState();
    setCustomMarker(mapMarker1, 'images/map/user_location.png');
    setCustomMarker(mapMarker2, 'images/map/safe_shelter.png');
    sheltersFuture = sheltersFutureBuilder();     // not needed
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Safe Shelters'),
        centerTitle: true,
        backgroundColor: Colors.red.shade300,
      ),
      body: SafeArea(
        child: FutureBuilder(
          future: sheltersFuture,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              String responseBody = snapshot.data.body;         // not needed
              var data = jsonDecode(responseBody);              // not needed 
   
              double userCurrentLatitude = data['current_loc']['latitude'];           // not needed
              double userCurrentLongitude = data['current_loc']['longitude'];         // not needed

              double nearestShelterLat = double.parse(data['shelters']['nearest_shelter'][0]['latitude']);    // not needed
              double nearestShelterLon = double.parse(data['shelters']['nearest_shelter'][0]['longitude']);   // not needed

              final Set<Marker> markers = {
                Marker(
                  markerId: MarkerId('user-loc'),
                  position: LatLng(userCurrentLatitude, userCurrentLongitude),
                  icon: mapMarker1,
                  infoWindow: InfoWindow(
                    title: 'You',
                    snippet: 'You are currently here!',
                  ),
                ),
                Marker(
                  markerId: MarkerId('nearest-shelter'),
                  position: LatLng(nearestShelterLat, nearestShelterLon),
                  icon: mapMarker2,
                  infoWindow: InfoWindow(
                    title: 'Nearest Safe Shelter',
                    snippet:
                        'This is the nearest safe shelter from your location',
                  ),
                ),
              };

              return GoogleMap(
                markers: markers,
                initialCameraPosition: CameraPosition(
                  target: LatLng(userCurrentLatitude, userCurrentLongitude),
                  zoom: 15,
                ),
              );
            }
          },
        ),
      ),
    );
  }
}

在上面的代码中,无论我如何尝试使用setCustomMarker我定义的函数,我在手机屏幕上看到的图标都是默认位置图标。

资产已正确添加到pubspec.yaml文件中。

如果有人可以向我指出我做错了什么以及如何解决这个问题,我会非常有帮助。谢谢。

4

0 回答 0