Showing a map with Google Maps

With everything ready for iOS and Android, we can now add a GoogleMap widget to the main screen of our app, as follows:

  1. To use a package, as usual, we can import it at the top of the file. So, let's include the required Google Maps dependency in the main.dart file, like this:
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
  1. Then, remove the stateful widget from the default app, and create a new stateful widget called MainMap, as follows:
class MainMap extends StatefulWidget {
@override
_MainMapState createState() => _MainMapState();
}

class _MainMapState extends State<MainMap> {
@override
Widget build(BuildContext context) {
return Container(
);
}}
  1. In the _MainMapState class, we'll return a Scaffold whose title is 'The Treasure Mapp' (I'm so proud of this app's nerdy title), and the body will contain a Container whose child will be a GoogleMap widget.
    GoogleMap is the object that shows a map on the screen. In our app, it will take all the available space. This is illustrated in the following block of code:
 Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('The Treasure Mapp'),),
body: Container(child: GoogleMap(),),
);
}

You may notice that the GoogleMap widget requires an initialCameraPosition parameter. This is the center of the map when we first show it to our user. Google uses geographic coordinates—latitude and longitude—to position a map or place markers on it.

The latitude/longitude system has been in use, with a few small changes, since the mathematician, astronomer, and geographer Claudius Ptolemy wrote the Geography world atlas in the year 150 AD. It's fascinating how two numbers can tell you exactly your position, wherever you are on earth.
Until recently, mariners have been the primary users of this system, but now, with the availability of GPS and easy access to maps, it's important you understand how this system works. If you want to learn more about this, have a look at https://gisgeography.com/latitude-longitude-coordinates/.

Let's temporarily create a fixed coordinate for the initialCameraPosition of our map. They are the coordinates of Rome, Italy, but feel free to choose your favorite position, possibly near your own location.

  1. A CameraPosition requires a target, which takes a LatLng to express the actual position. Optionally, you can also specify a zoom level—the bigger the number, the higher the scale of the map. We'll begin with a zoom level of 12. At the top of the _MainMapState class, let's write the following code:
 final CameraPosition position = CameraPosition(
target: LatLng(41.9028, 12.4964),
zoom: 12,
);
  1. Then, in the GoogleMap constructor, we can specify the initialCameraPosition, as follows:
      body: Container(
child: GoogleMap(
initialCameraPosition: position,
),),
  1. If you try out the app right now, you should see a map similar to the one in the following screenshot: 

This means that everything is working so far. The next step will be adding a marker to the map, which highlights our position.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.217.253