Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GreyHat147/3ea92f4e962218893b84af667452b087 to your computer and use it in GitHub Desktop.
Save GreyHat147/3ea92f4e962218893b84af667452b087 to your computer and use it in GitHub Desktop.
Map page
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:location/location.dart';
import '../services/promotion.dart';
class MapPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => new _MapPageState();
}
class _MapPageState extends State<MapPage> {
Completer<GoogleMapController> _controller = Completer();
final Set<Marker> _markers = {};
bool _isLoading = false;
LocationData _startLocation;
LocationData _currentLocation;
StreamSubscription<LocationData> _locationSubscription;
Location _locationService = new Location();
bool _permission = false;
String error;
PromotionService _promotionService;
@override
void initState() {
//super.initState();
print('init');
_promotionService = PromotionService();
initPlatformState();
}
getStores(String latitude, String longitude) {
_isLoading = true;
_promotionService.getStores(latitude, longitude).then((result) {
_isLoading = false;
// print((result));
for (final i in result["data"]) {
// print('$i');
var latitude = double.parse(i["coords"]["latitude"]);
var longitude = double.parse(i["coords"]["longitude"]);
setState(() {
_markers.add(Marker(
// This marker id can be anything that uniquely identifies each marker.
markerId: MarkerId(i["key"]),
position: LatLng(latitude, longitude),
infoWindow: InfoWindow(
title: i["business_name"],
snippet: i["addreess"],
),
icon: BitmapDescriptor.defaultMarker));
});
}
}).catchError((onError) {
_isLoading = false;
print(onError);
});
}
initPlatformState() async {
await _locationService.changeSettings(
accuracy: LocationAccuracy.HIGH, interval: 1000);
LocationData location;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
bool serviceStatus = await _locationService.serviceEnabled();
print("Service status: $serviceStatus");
if (serviceStatus) {
_permission = await _locationService.requestPermission();
print("Permission: $_permission");
if (_permission) {
location = await _locationService.getLocation();
String latitude = location.latitude.toString();
String longitude = location.longitude.toString();
print('$latitude $longitude');
getStores(latitude, longitude);
}
} else {
bool serviceStatusResult = await _locationService.requestService();
print("Service status activated after request: $serviceStatusResult");
if (serviceStatusResult) {
initPlatformState();
}
}
} on PlatformException catch (e) {
print(e);
if (e.code == 'PERMISSION_DENIED') {
error = e.message;
} else if (e.code == 'SERVICE_STATUS_ERROR') {
error = e.message;
}
location = null;
}
setState(() {
_startLocation = location;
});
}
static final CameraPosition _kGooglePlex = CameraPosition(
target: LatLng(4.672414, -74.063829),
zoom: 14.4746,
);
@override
Widget build(BuildContext context) {
return new CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
padding: new EdgeInsetsDirectional.only(start: 10.0, end: 20.0),
backgroundColor: new Color.fromRGBO(148, 3, 123, 1.0),
middle: new Text('Mapa', style: new TextStyle(color: CupertinoColors.white, fontFamily: 'Raleway', letterSpacing: 1),),
),
child: Stack(
children: <Widget>[_showMap(), _showCircularProgress()],
));
}
Widget _showMap() {
return Container(
height: MediaQuery.of(context).size.height - 118,
child: GoogleMap(
mapType: MapType.normal,
initialCameraPosition: _kGooglePlex,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
markers: _markers,
),
);
}
Widget _showCircularProgress() {
if (_isLoading) {
return Center(
child: CupertinoActivityIndicator(
animating: true,
radius: 15.0,
));
}
return Container(height: 0.0, width: 0.0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment