Google Map and Flutter App service provider

Integrating Maps In Flutter

Map and Flutter App integration was done in one of our projects by Sreyas. The process typically involves using a plugin to interact with native map SDKs like Google Maps.

The Steps to Integrate Map in Flutter:

Follow the Procedure For Integrating Maps In Flutter Apps :

1. Choose a Map Provider

Google Maps and Mapbox are popular choices. Choose based on features, pricing, and the specific needs of your app.

2. Set Up Dependencies

In your pubspec.yaml file, add the necessary dependencies:

Dependencies:

  • flutter:
  • sdk: flutter
  • google_maps_flutter: ^2.0.10  # For Google Maps
  • mapbox_gl: ^2.0.0 # For Mapbox

Run flutter pub get to install these dependencies.

3. Configure API Keys

  • Google Maps: Obtain an API key from the Google Cloud Console (enable Google Maps SDK for Android and iOS).
  • Mapbox: Sign up on the Mapbox website to get an access token.

4. Android Configuration

Google Maps:
  • Add your Google Maps API key to android/app/src/main/AndroidManifest.xml:
<manifest ...>
  <application ...>
    <meta-data
      android:name="com.google.android.geo.API_KEY"
      android:value="YOUR_API_KEY_HERE"/>
  </application>
</manifest>

5. iOS Configuration

Google Maps:

Add your Google Maps API key to ios/Runner/AppDelegate.swift:

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    // Add the following line with your API key
    GMSServices.provideAPIKey("YOUR_API_KEY_HERE")
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

6. Implementing the Map

Google Maps:
import 'package:google_maps_flutter/google_maps_flutter.dart';

GoogleMap(
  initialCameraPosition: CameraPosition(
    target: LatLng(37.7749, -122.4194),
    zoom: 12,
  ),
  markers: Set<Marker>.from([
    Marker(
      markerId: MarkerId("marker_1"),
      position: LatLng(37.7749, -122.4194),
      infoWindow: InfoWindow(
        title: "San Francisco",
        snippet: "An interesting city",
      ),
    ),
  ]),
  onMapCreated: (GoogleMapController controller) {
    // You can manipulate the map controller here
  },
)

Key Points to Consider:

  • API Key/Access Token: Always keep your keys/tokens secure and do not expose them in public repositories.
  • Permissions: Ensure your app has the necessary location permissions for optimal map functionality.
  • Map Controls: Familiarize yourself with controlling the map view (zoom, pan) programmatically.
  • Markers and Overlays: Use markers, polylines, polygons, etc., to enhance user interaction and data visualization.
  • Handling Events: Implement callbacks to handle map events like taps, camera movements, etc.

By following these steps and considerations, you should be able to integrate maps into your Flutter application effectively using either Google Maps. Adjustments may be needed based on specific use cases or additional features required by your application.

  Fetching The Current Location Using GeoLocator

The necessary setup and code implementation using the geolocator package, which is one of the most commonly used packages for accessing location services in Flutter.

Follow the Step-by-Step Procedure:

1. Add Dependencies:

  • Open your pubspec.yaml file.
  • Add the Dependencies: geolocator dependency under dependencies: $flutter pub add geolocator

2. Update Android Configuration:

  • Open the android/app/src/main/AndroidManifest.xml file and add the following permissions inside the <manifest> tag:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

3. Update iOS Configuration:

  • Open the ios/Runner/Info.plist file and add the following keys for location permissions:
<key>NSLocationWhenInUseUsageDescription</key>
<string>We need your location to show your current position on the map.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>We need your location to keep track of your movements in the background.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>We need your location to provide full functionality.</string>

4. Request Location Permission:

  • Create a function to request location permission from the user.
  • Import the necessary package: import 'package:geolocator/geolocator.dart';
  • Define a function to request permission.
Future<void> _requestPermission() async {
LocationPermission permission = await Geolocator.checkPermission();
 if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
 if (permission == LocationPermission.denied) {
// Handle the case when permission is denied
   return;
 }
}
 if (permission == LocationPermission.deniedForever) {
// Handle the case when permission is permanently denied 
return;
 }
  // Permission granted, proceed with fetching the location 
}

5. Fetch the Current Location:

  • Here we are giving the Function to get the current location using the geolocator Package.
Future<Position> _getCurrentLocation() async {
 bool serviceEnabled;
 LocationPermission permission;
 // Check if location services are enabled
 serviceEnabled = await Geolocator.isLocationServiceEnabled();
 if (!serviceEnabled) {
 // Location services are not enabled, handle accordingly
return Future.error('Location services are disabled.');
 } 
// Request permission if needed
  permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
   permission = await Geolocator.requestPermission();
 if (permission == LocationPermission.denied) {
 // Permissions are denied, handle accordingly
     return Future.error('Location permissions are denied');
 } 
} 
  if (permission == LocationPermission.deniedForever) {
  // Permissions are permanently denied, handle accordingly
    return Future.error('Location permissions are permanently denied, we cannot request permissions.');
  } 
  // When we reach here, permissions are granted and we can fetch the location
  return await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
}

6. Use the Location Data:

  • You can call the _getCurrentLocation() function in your app to get the current location.
void _showCurrentLocation() async {
 try {
 Position position = await _getCurrentLocation();
  print('Current location: ${position.latitude}, ${position.longitude}');
// You can update the UI or perform other actions with the location data
catch (e) {
print('Error fetching location: $e'); 
}
} 
}

7. Handle Permissions and Errors:

  • Make sure to handle various scenarios where location permissions are denied or location services are disabled.

Follow These steps for integrating the current location using Geolocator.

Clients looking for Map and Flutter App integration service provider can reach out to Sreyas. We offer comprehensive solutions for map integration, from initial setup and configuration to advanced customizations. Our expertise includes Custom Markers and Overlays, Real-time Location Tracking, Geolocation Services, Map Styling and theming, Performance Optimization, and Event Handling.

At Sreyas, we pride ourselves on delivering high-quality solutions that meet the unique requirements of each client. Our experienced team stays updated with the latest advancements in Flutter and map integration technologies, ensuring that we provide cutting-edge solutions.

Whether you are developing a new application or enhancing an existing one, our services are designed to provide seamless integration of mapping functionalities. By leveraging our expertise, you can offer your users a feature-rich, intuitive, and engaging map experience.

For businesses in the US, particularly in states like California, New York, Texas, Florida, and others. In the UK states like Denmark, Finland, France, Sweden, Switzerland, Norway, and others. Sreyas is your go-to partner for all your map integration needs in Flutter mobile applications. Contact us today to learn how we can help elevate your app’s capabilities and provide a superior user experience.

Recent Blogs


Posted

in

by

Tags:

To Know Us Better

Browse through our work.

Explore The Technology Used

Learn about the cutting-edge technology and techniques we use to create innovative software solutions.