skip to Main Content

enter image description here

I am trying to use google map API to show pharmacies Near me and all that in a flutter app , I configured everything and got the API Key …. I also managed to ask for GPS Permission and it worked , but whenever I Accept the Permission and wait for the app to Load I get this error message, I tried ChatGPT but nothing ,

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';


class Pharmacy {
  final String name;
  final String address;
  final double distance;
  final double latitude;
  final double longitude;

  Pharmacy({
    required this.name,
    required this.address,
    required this.distance,
    required this.latitude,
    required this.longitude,
  });
}

class PharmaciesScreen extends StatefulWidget {
  @override
  _PharmaciesScreenState createState() => _PharmaciesScreenState();
}

class _PharmaciesScreenState extends State<PharmaciesScreen> {
  Completer<GoogleMapController> _controller = Completer();
  Set<Marker> _markers = {};

  Position? _currentPosition;

  List<Pharmacy> pharmacies = [
    Pharmacy(name: 'Pharmacy 1', address: '123 Main St', distance: 1.2, latitude: 37.7749, longitude: -122.4194),
    Pharmacy(name: 'Pharmacy 2', address: '456 Maple Ave', distance: 2.5, latitude: 37.7894, longitude: -122.4062),
    Pharmacy(name: 'Pharmacy 3', address: '789 Oak Dr', distance: 3.7, latitude: 37.8030, longitude: -122.4419),
    // Add more pharmacies as needed
  ];

  @override
  void initState() {
    super.initState();
    _getCurrentLocation();
  }

  Future<void> _getCurrentLocation() async {
    Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    setState(() {
      _currentPosition = position;
    });
  }

  @override
  Widget build(BuildContext context) {
    bool isDarkMode = Theme.of(context).brightness == Brightness.dark;
    Color backgroundColor = isDarkMode ? Colors.grey[850]! : Colors.white;
    Color textColor = isDarkMode ? Colors.white : Colors.black;

    return Scaffold(
      appBar: AppBar(
        title: Text('Pharmacies Near Me', style: TextStyle(color: textColor)),
        backgroundColor: Color(0xFFFC7B32), // Main color of the app
      ),
      body: _currentPosition != null
          ? Stack(
        children: [
          GoogleMap(
            mapType: MapType.normal,
            initialCameraPosition: CameraPosition(
              target: LatLng(_currentPosition!.latitude, _currentPosition!.longitude),
              zoom: 14.0,
            ),
            markers: _markers,
            onMapCreated: (GoogleMapController controller) {
              _controller.complete(controller);
              setState(() {
                _markers.clear();
                for (var pharmacy in pharmacies) {
                  _markers.add(
                    Marker(
                      markerId: MarkerId(pharmacy.name),
                      position: LatLng(pharmacy.latitude, pharmacy.longitude),
                      infoWindow: InfoWindow(
                        title: pharmacy.name,
                        snippet: pharmacy.address,
                      ),
                      onTap: () {
                        // Add logic to open Google Maps with directions to this pharmacy
                      },
                    ),
                  );
                }
              });
            },
          ),
          Positioned(
            bottom: 16.0,
            left: 16.0,
            right: 16.0,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                ElevatedButton(
                  onPressed: () {
                    // Add logic to filter pharmacies by 'Near Me' here
                  },
                  child: Text('Near Me', style: TextStyle(color: textColor)),
                  style: ElevatedButton.styleFrom(
                    primary: Color(0xFFFC7B32), // Main color of the app
                  ),
                ),
                ElevatedButton(
                  onPressed: () {
                    // Add logic to filter pharmacies by '24/7' here
                  },
                  child: Text('24/7', style: TextStyle(color: textColor)),
                  style: ElevatedButton.styleFrom(
                    primary: Color(0xFFFC7B32), // Main color of the app
                  ),
                ),
              ],
            ),
          ),
        ],
      )
          : Center(
        child: CircularProgressIndicator(),
      ),
    );
  }
}

2

Answers


  1. You’re going to have to share some code for me to help you

    Login or Signup to reply.
  2. I was able to replicate the issue you encountered and successfully solved it by ensuring the proper initialization of the Google Maps JavaScript API.
    enter image description here

    Since you are deploying your Flutter app on the web platform, it is essential to dynamically load the Google Maps JavaScript API into your webpage. This can be achieved by adding the following script tag to the web/index.html file of your Flutter Web project:

    <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"
    async defer>
    

    By including this script tag, you ensure that the necessary JavaScript code for the Google Maps API is loaded and available when your Flutter web app is running.

    Note: You may have followed different steps to load the Google Maps API, such as adding your API key in your manifest file, which is a common practice for the Android platform. Therefore, it’s important to note that integrating maps into your Flutter app may involve platform-specific instructions. Be sure to always check each library’s official documentation, as this information is also included in the documentation for google_maps_flutter on pub.dev.

    Lastly, you mentioned wanting to show nearby pharmacies based on your location. Currently you’re manually adding them to your List<Pharmacy> pharmacies = []. For a more dynamic approach, consider using the Nearby Search feature of the Google Places API.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search