Skip to content

Commit

Permalink
updated mapPage
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremiasBulanadi committed Sep 11, 2021
1 parent 71f6a0e commit 22b2a88
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 45 deletions.
11 changes: 9 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import 'package:flutter/material.dart';
import 'package:news_appdate/providers/news_provider.dart';
import 'package:provider/provider.dart';
import 'package:location/location.dart';
import './services/geocoding.dart';
import './services/auth.dart';
import './models/user.dart';
import './pages/wrapper.dart';
Expand All @@ -16,14 +18,19 @@ import './providers/news_provider.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
AuthService _auth = AuthService();

runApp(MultiProvider(
providers: [
StreamProvider<AppUser?>(
create: (_) => _auth.user,
initialData: null,
),
ChangeNotifierProvider(create: (BuildContext context) => NewsProvider())
ChangeNotifierProvider(create: (BuildContext context) => NewsProvider()),
FutureProvider<LocationData?>(
create: (_) {
return getUserLocation();
},
initialData: null,
)
],
child: Wrapper(),
));
Expand Down
80 changes: 37 additions & 43 deletions lib/pages/mapPage.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:provider/provider.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:flutter/material.dart';
Expand All @@ -19,20 +20,15 @@ class MapPage extends StatefulWidget {
}

class _MapPageState extends State<MapPage> {
loc.LocationData? userLocation;
bool hasSetInitialLocation = false;
List<Marker> markers = [];
LatLng? _initialLocation;
StreamSubscription? _locationSubscription;
//StreamSubscription? _locationSubscription;
BitmapDescriptor? pinLocation;
loc.Location location = new loc.Location();
loc.Location _locationTracker = loc.Location();
//loc.Location _locationTracker = loc.Location();
GoogleMapController? _controller;
Position? _currentPosition;
Position? first_coords;
@override
static final CameraPosition initialLocation = CameraPosition(
target: LatLng(15.132722, 120.589111),
zoom: 15,
);

void updateMarker(loc.LocationData newLocationData) {
Geolocator.getCurrentPosition().then((Position position) {
Expand All @@ -45,42 +41,26 @@ class _MapPageState extends State<MapPage> {
LatLng(_currentPosition!.latitude, _currentPosition!.longitude);
this.setState(() {
markers.add(Marker(
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueBlue),
markerId: MarkerId('mylocation'),
icon:
BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueOrange),
markerId: MarkerId('MY LOCATION'),
position: latLng,
draggable: false));
});
}

void mapLocation() async {
try {
var location = await _locationTracker.getLocation();

updateMarker(location);

if (_locationSubscription != null) {
_locationSubscription!.cancel();
}
_locationSubscription =
_locationTracker.onLocationChanged.listen((newLocalData) {
if (_controller != null) {
_controller!.animateCamera(CameraUpdate.newCameraPosition(
CameraPosition(
bearing: 192.8334901395799,
target: LatLng(
_currentPosition!.latitude, _currentPosition!.longitude),
tilt: 0,
zoom: 18.00)));
updateMarker(newLocalData);
}
});
} on PlatformException catch (e) {
if (e.code == 'PERMISSION_DENIED') {}
void focusOnUser() {
if (_controller != null) {
_controller!.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
target:
LatLng(userLocation?.latitude ?? 0, userLocation?.longitude ?? 0),
zoom: 10)));
}
}

setMarkers() async {
markers = [];

NewsData? newsData = context.watch<NewsProvider>().newsData;
if (newsData.locationalNews != null &&
newsData.locationalNews!.length > 0) {
Expand All @@ -103,14 +83,14 @@ class _MapPageState extends State<MapPage> {
}
//final query = _news_story!.locations!.first.text;
//var address = await locationFromAddress(query);
//first_coords = address as Position?;
//firstCoords = address as Position?;
}

addMarkers(List<Story>? stories, NewsListType newsListType) {
BitmapDescriptor markerIcon;
if (newsListType == NewsListType.locational) {
markerIcon =
BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueRed);
BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueRose);
} else if (newsListType == NewsListType.recommended) {
markerIcon =
BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueGreen);
Expand Down Expand Up @@ -151,13 +131,27 @@ class _MapPageState extends State<MapPage> {
}

Widget build(BuildContext context) {
// This part isnt fast enough to get user's location
userLocation = Provider.of<loc.LocationData?>(context);
markers.add(new Marker(
markerId: MarkerId("userLocation"),
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueOrange),
infoWindow: InfoWindow(
title: "Your Location",
snippet:
"${userLocation?.latitude ?? "?"}, ${userLocation?.longitude ?? "?"}"),
));

setMarkers();
return Scaffold(
body: Stack(
children: [
GoogleMap(
zoomControlsEnabled: false,
initialCameraPosition: initialLocation,
initialCameraPosition: CameraPosition(
target: LatLng(
userLocation?.latitude ?? 0, userLocation?.longitude ?? 0),
zoom: 0),
markers: Set.of(markers),
onMapCreated: (GoogleMapController controller) {
_controller = controller;
Expand All @@ -170,16 +164,16 @@ class _MapPageState extends State<MapPage> {
color: Colors.green,
textColor: Colors.white,
child: Icon(
Icons.location_searching,
Icons.my_location,
size: 24,
),
padding: EdgeInsets.all(16),
shape: CircleBorder(),
onPressed: () {
mapLocation();
for (Marker marker in markers) {
print(marker.position.toString());
}
focusOnUser();
// for (Marker marker in markers) {
// print(marker.position.toString());
// }
}),
),
],
Expand Down

0 comments on commit 22b2a88

Please sign in to comment.