1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-08 13:58:47 +00:00

Added Google Maps sample code (#1671)

* Added Google Maps sample code

* Updated per @domesticmouse review

* Updated dependabot and pubspec for Dart 2.19
This commit is contained in:
Anthony Sansone
2023-02-27 18:16:45 -06:00
committed by GitHub
parent b52d7f7744
commit 64d10ca175
76 changed files with 2655 additions and 0 deletions

62
google_maps/lib/main.dart Normal file
View File

@@ -0,0 +1,62 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late GoogleMapController mapController;
final LatLng _center = const LatLng(-33.86, 151.20);
void _onMapCreated(GoogleMapController controller) {
mapController = controller;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Sydney'),
backgroundColor: Colors.green[700],
),
body: GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: _center,
zoom: 11.0,
),
markers: {
// ignore: prefer_const_constructors
Marker(
markerId: const MarkerId('Sydney'),
position: const LatLng(-33.86, 151.20),
)
}),
),
);
}
}

View File

@@ -0,0 +1,121 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:http/http.dart' as http;
import 'package:json_annotation/json_annotation.dart';
part 'locations.g.dart';
@JsonSerializable()
class LatLng {
LatLng({
required this.lat,
required this.lng,
});
factory LatLng.fromJson(Map<String, dynamic> json) => _$LatLngFromJson(json);
Map<String, dynamic> toJson() => _$LatLngToJson(this);
final double lat;
final double lng;
}
@JsonSerializable()
class Region {
Region({
required this.coords,
required this.id,
required this.name,
required this.zoom,
});
factory Region.fromJson(Map<String, dynamic> json) => _$RegionFromJson(json);
Map<String, dynamic> toJson() => _$RegionToJson(this);
final LatLng coords;
final String id;
final String name;
final double zoom;
}
@JsonSerializable()
class Office {
Office({
required this.address,
required this.id,
required this.image,
required this.lat,
required this.lng,
required this.name,
required this.phone,
required this.region,
});
factory Office.fromJson(Map<String, dynamic> json) => _$OfficeFromJson(json);
Map<String, dynamic> toJson() => _$OfficeToJson(this);
final String address;
final String id;
final String image;
final double lat;
final double lng;
final String name;
final String phone;
final String region;
}
@JsonSerializable()
class Locations {
Locations({
required this.offices,
required this.regions,
});
factory Locations.fromJson(Map<String, dynamic> json) =>
_$LocationsFromJson(json);
Map<String, dynamic> toJson() => _$LocationsToJson(this);
final List<Office> offices;
final List<Region> regions;
}
Future<Locations> getGoogleOffices() async {
const googleLocationsURL = 'https://about.google/static/data/locations.json';
// Retrieve the locations of Google offices
try {
final response = await http.get(Uri.parse(googleLocationsURL));
if (response.statusCode == 200) {
return Locations.fromJson(
json.decode(response.body) as Map<String, dynamic>);
}
} catch (e) {
if (kDebugMode) {
print(e);
}
}
// Fallback for when the above HTTP request fails.
return Locations.fromJson(
json.decode(
await rootBundle.loadString('assets/locations.json'),
) as Map<String, dynamic>,
);
}

View File

@@ -0,0 +1,83 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'locations.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
LatLng _$LatLngFromJson(Map<String, dynamic> json) => LatLng(
lat: (json['lat'] as num).toDouble(),
lng: (json['lng'] as num).toDouble(),
);
Map<String, dynamic> _$LatLngToJson(LatLng instance) => <String, dynamic>{
'lat': instance.lat,
'lng': instance.lng,
};
Region _$RegionFromJson(Map<String, dynamic> json) => Region(
coords: LatLng.fromJson(json['coords'] as Map<String, dynamic>),
id: json['id'] as String,
name: json['name'] as String,
zoom: (json['zoom'] as num).toDouble(),
);
Map<String, dynamic> _$RegionToJson(Region instance) => <String, dynamic>{
'coords': instance.coords,
'id': instance.id,
'name': instance.name,
'zoom': instance.zoom,
};
Office _$OfficeFromJson(Map<String, dynamic> json) => Office(
address: json['address'] as String,
id: json['id'] as String,
image: json['image'] as String,
lat: (json['lat'] as num).toDouble(),
lng: (json['lng'] as num).toDouble(),
name: json['name'] as String,
phone: json['phone'] as String,
region: json['region'] as String,
);
Map<String, dynamic> _$OfficeToJson(Office instance) => <String, dynamic>{
'address': instance.address,
'id': instance.id,
'image': instance.image,
'lat': instance.lat,
'lng': instance.lng,
'name': instance.name,
'phone': instance.phone,
'region': instance.region,
};
Locations _$LocationsFromJson(Map<String, dynamic> json) => Locations(
offices: (json['offices'] as List<dynamic>)
.map((e) => Office.fromJson(e as Map<String, dynamic>))
.toList(),
regions: (json['regions'] as List<dynamic>)
.map((e) => Region.fromJson(e as Map<String, dynamic>))
.toList(),
);
Map<String, dynamic> _$LocationsToJson(Locations instance) => <String, dynamic>{
'offices': instance.offices,
'regions': instance.regions,
};