Home > Back-end >  direct location using google maps application
direct location using google maps application

Time:02-02

  CollectionReference _reference = FirebaseFirestore.instance.collection('products');
 
  List<AddressModel> sellerLocation = [];
  //late List product = [];
  late CustAddressModel customerLocation;
  List<AddressModel> placesNear = []; //filter on category
  late List product = []; // retrieve all data from firebase included in List so just call List
  late List productNear = [];
  late List distanceList = [];


  // this is location 1 (seller location)
  Future sellLocation(String id) async{
    final CollectionReference ref =  FirebaseFirestore.instance.collection('sellerProfile');
    ref.doc(id).get().then((ds) {
      var temp = {
        'latitude': ds['latitude'],
        'longitude': ds['longitude'],
        'address': ds['address'],
        'postcode': ds['postcode'],
        'city': ds['city'],
        'state': ds['state'],
        'shopName': ds['shopName'],
        'id': ds.id

      };
      var tempAddress = temp['address']   temp['postcode']   temp['city']   temp['state'];
      AddressModel tempo = AddressModel(latitude: temp['latitude'],
          longitude: temp['longitude'],
          address: tempAddress,
          id:temp['id'],
          shopName:temp['shopName'],
      );
      setState(() {
        sellerLocation.add(tempo);
      });
    });
  }


  // this is location 2 (customer location)
  Future custLocation() async {
    final CollectionReference ref =  FirebaseFirestore.instance.collection('custLocation');
    ref.get().then((ds) {
      ds.docs.forEach((data) {
        var temp = {
          'latitude': data['latitude'],
          'longitude': data['longitude'],
          'address': data['address'],
        };

        setState(() {
          customerLocation = CustAddressModel(latitude: temp['latitude'], longitude: temp['longitude'], address: temp['address'],);
        });
      });
    });
  }

  List<AddressModel> getDistance(List<AddressModel>address, double filterNear) {
    List<AddressModel> tempList = [];
    for(int i=0;i<address.length;i  ){
      double distance = Geolocator.distanceBetween(
          customerLocation.latitude!, customerLocation.longitude!, address.elementAt(i).latitude!,
          address.elementAt(i).longitude!);
      if(distance <= filterNear){
        // change to km
        var distanceInKM = distance/1000;
        distanceList.add(distanceInKM.toStringAsFixed(1));
        tempList.add(address.elementAt(i));
      }
    }
    return tempList;
  }


  // custom function to locate the location to direct using google maps app
  Future<void> openMap(List<CustAddressModel>latitude,longitude) async {
    String googleUrl = 'https://www.google.com/maps/search/?api=1&query=$latitude,$longitude';

    await canLaunchUrlString(googleUrl)
    ? await launchUrlString(googleUrl)
        : throw 'Could not launch google map $googleUrl';
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    custLocation();

    }
  }

Here i want to custom function openMap() where user can click button to direct the location using google maps app. Location 1 where the seller location i retrieve from database and the location 2 is customer location i retrieve from database also. Now i want to pass the lat and long from both location 1 and 2, so that user can direct to google maps app to direct to seller location

CodePudding user response:

you can use the packege url_launcher and then add following code:

import 'package:url_launcher/url_launcher.dart';

class MapUtils {

  MapUtils._();

  static Future<void> openMap(double latitude, double longitude) async {
    String googleUrl = 'https://www.google.com/maps/search/?api=1&query=$latitude,$longitude';
    if (await canLaunch(googleUrl)) {
      await launch(googleUrl);
    } else {
      throw 'Could not open the map.';
    }
  }
}

Now you can open google maps in your app just call this method:

onTap: () {
   final snapshot = await Firestore.instance.collection("collectionName").document('docId').get();
  final lat = snapshot.data['latitude'];
  final long = snapshot.data['longitude'];
   MapUtils.openMap(lat, long);
};

On iOS you need to do some extra steps is that, write following lines in info.plist file

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>googlechromes</string>
    <string>comgooglemaps</string>
</array>
  • Related