Trump and Biden Reenact the Battle of the Alamo

This is an unconventional Medium story. The idea to send Donald Trump and Joe Biden back in time to the Battle of the Alamo sprung after the recent “Trump Train” versus “Biden Bus” incident in Texas…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Exploring Google Maps in Flutter

Adding Officially Supported Google Maps in a Flutter app

This article will look at how to add Google Maps into a Flutter project and the features and customization available. At the time of writing, this plugin is still in developer preview and may have breaking changes in the future. I will try my best to update the article whenever required.

Note: Since Google Maps relies on an external package and is not inbuilt, I haven’t added this article as part of my Deep Dive series into widgets. Nevertheless, this article will follow a similar pattern.

In a nutshell:

3. Add key to iOS:

Add the GoogleMaps import to Runner -> AppDelegate.m and add the API Key. Your file should look like this:

4. Add this to Info.plist

And you’re set!

To add a Google Map, use a GoogleMap widget in your widget tree. Here is a basic example:

This gives us:

A simple implementation of Google Maps in a Flutter app

This is pretty good for just a few lines of code. But most times, we don’t want an app to have a random map. So, let’s see now how to customize, control the map, add markers, etc.

The options parameter allows us to set some default settings like enabling/disabling gestures or setting the default camera position, etc.

We can set the map type using the mapType parameter in GoogleMapOptions.

This can be set to satellite, hybrid, normal or terrain.

Switching to satellite view

Setting the cameraPosition parameter sets the default camera position to a target. We will look at this in detail when we look at CameraPosition in the next section.

The user’s location can be shown on the map by setting the myLocationEnabled parameter to true. For this to work we must also add the required permissions on both platforms.

On Android:

Add

or

to manifest.xml. We also need to explicitly ask the user for permission.

On iOS:

Add a ‘NSLocationWhenInUseUsageDescription’ key to your ‘Info.plist’ file. This will automatically prompt the user for permissions when the map tries to turn on the My Location layer.

The user’s location is now displayed

If all goes well, this should be the result.

GoogleMapOptions gives us various options to either enable or disable specific types of gestures like tilt, zoom, etc.

This code snippet disables the three gestures mentioned.

If you noticed, the onMapCreated method in the GoogleMap gave us a GoogleMapController.

We can use this controller to do things like set markers or move the camera around.

Let’s see an example of moving the camera to a specific location, say, the Google HQ.

A variable is used to store an instance of GoogleMapController, allowing us to animate the camera.

Next, we have a FloatingActionButton and with its onPressed set to animate the camera to the latitude and longitude of the Google HQ.

Animating the camera to a specific latitude and longitude

Let’s look at all the other things we can do with moving the camera.

This method lets us set the latitude and longitude, zoom, bearing (the orientation of the map in degrees) and tilt (a higher tilt gives a side-view/tilted-view of buildings). Bearing changes the direction the camera points in and not tilt.

This gives us:

Using .newCameraPosition() to update camera view

This sets the camera between two LatLngs.

For example, it makes more sense for an app like Uber to show enough of the map to cover you and the car tasked with picking you up rather than showing a map of the entire city.

The function takes the southwest point and the northeast point to cover on the map.

The second parameter is a padding, let’s see what it does.

Here the southwest point is the city of Paris and northeast is Brussels. Let’s set the padding to 0.0 and try it out.

The cities are barely visible. To avoid this, we give the map a higher padding to bring the cities in the map.

Padding 48.0

This function simply sets the camera to a new latitude and longitude while retaining the same zoom factor.

This is similar to the last function but also allows us to change the zoom factor of the camera.

Simply scrolls the map in the X and/or Y direction by a certain amount.

The first parameter gives shift in X direction whereas the second gives shift in the Y direction.

Simply zooms in or out.

Zooms by the amount given.

Zooms to the specific factor.

We can use the same GoogleMapController we used earlier to add markers on the map as well.

This is as easy as:

We can add information to markers using the infoWindowText parameter.

Adding information to a marker

We can change the image being used for the marker using the icon parameter. Let’s use the Flutter icon as a marker.

In the icon parameter we can supply an asset like this:

Using a Flutter icon as a marker

We can change color using the icon parameter as well:

We can set the alpha (how opaque the icon is) as well:

And finally, we can rotate the icon as well:

Feel free to check out my other profiles and articles as well:

Add a comment

Related posts:

Conclusion

Meta transactions allow apps to abstract away seed phrases and wallet downloads at first. Users can simply use an app and the notion of gas is abstracted away. Then, after they have earned value…

How To Use A Rewriting API To Refresh Your Blogs

You probably feel a sense of accomplishment when you publish a post and cross it off your SEO to-do list. However, once your content is published, it’s easy to lose sight of it as you work on the…

Systemic racism in science

In light of recent events, GMOP is focusing on systemic racism pervading science in this week’s post. It should go without saying that Black lives matter. Yet America is still suffering from…