How to call latitude and longitude,Mysql flutter

40 Views Asked by At

I'm just learning flutter and still lack experience I'm making a geofencing project, I take the teacher's position using a button then save it in mysql, I call the teacher's position in the admin, the application uses 2 logins

listLive.php

<?php 

    $connection = new mysqli("localhost","root","","test");
    $data       = mysqli_query($connection, "select * from teacher_position");
    $data       = mysqli_fetch_all($data, MYSQLI_ASSOC);

    echo json_encode($data);
?>

admin page that displays Google Maps, I want this page to display table rows (name, latitude and longitude),admin page that displays Google Maps, I want this page to display table rows (name, latitude and longitude), as well as a button to open Google Maps by reading the latitude and longitude values

import 'dart:async';
import 'dart:collection';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:location/location.dart';

class AdminMap extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<AdminMap> {
  late Future _future;
  final Set<Marker> _markers = HashSet<Marker>();
  var nama = TextEditingController();
  var latitude = TextEditingController();
  var longitude = TextEditingController();

  Future<double> loadString() async {
    var response =
        await http.get(Uri.parse("http://10.0.2.2/sdn06perawang/listLive.php"));
    final jsonresponse = jsonDecode(response.body);
    //print(responsebody);

    for (int i = 0; i < jsonresponse.length; i++) {
      LatLng latlng = LatLng(double.parse(jsonresponse['latitude']),
          double.parse(jsonresponse['longitude']));
      final _kGoogle = CameraPosition(target: latlng, zoom: 20);
    }
    return jsonresponse;
  }

  _getData() async {
    try {
      final response = await http.get(Uri.parse(
          //you have to take the ip address of your computer.
          //because using localhost will cause an error
          //get detail data with id
          "http://10.0.2.2/sdn06perawang/listLive.php"));

      // if response successful
      if (response.statusCode == 200) {
        final data = jsonDecode(response.body);

        setState(() {
          nama = TextEditingController(text: data['nama']);
          latitude = TextEditingController(text: data['latitude']);
          longitude = TextEditingController(text: data['longitude']);
        });
        for (int i = 0; i < data.length; i++) {
          LatLng latlng = LatLng(
              double.parse(data['latitude']), double.parse(data['longitude']));
          final _kGoogle = CameraPosition(target: latlng, zoom: 20);
        }
      }
    } catch (e) {
      print(e);
    }
  }

  final Completer<GoogleMapController> _controller = Completer();

  @override
  void initState() {
    super.initState();
    loadString();
    _markers.add(Marker(
        markerId: const MarkerId("0"),
        position: const LatLng(0.6710907654749535, 101.58716319897873),
        draggable: true,
        onDragEnd: (value) {}));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(children: [
        Container(
          height: MediaQuery.of(context).size.height,
          width: MediaQuery.of(context).size.width,
          child: FutureBuilder(
            future: loadString(),
            builder: (context, AsyncSnapshot snapshot) {
              if (snapshot.hasData == Null) {
                return const Center(
                  child: CircularProgressIndicator(),
                );
              }

              return GoogleMap(
                markers: _markers,
                onMapCreated: (GoogleMapController controller) {
                  _controller.complete(controller);
                },
                initialCameraPosition: loadString()._kGoogle,
                mapType: MapType.hybrid,
                tiltGesturesEnabled: true,
                compassEnabled: true,
                rotateGesturesEnabled: true,
                myLocationEnabled: true,
              );
            },
          ),
        ),
      ]),
    );
  }
}

help me to animateCamera and CameraPosition

0

There are 0 best solutions below