Android Studio Google Map
Syntax
To use Google Maps in your Android Studio project, you will need to follow these steps:
- Include the Google Play Services Library in your project:
dependencies {
implementation 'com.google.android.gms:play-services-maps:17.0.0'
}
- Add the API key provided by Google:
<application>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="[YOUR API KEY]" />
</application>
- In your activity's layout XML file, add a
MapView
object:
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
- In your activity, you need to initialize the
MapView
:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapView mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
}
Example
Here is an example code for showing a Google Map:
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapView mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
Output
When you run this code, you will see a Google Map with a marker pointing at Sydney, Australia.
Explanation
In this example, we create a new activity called MainActivity which implements OnMapReadyCallback. This means that when the map is ready to be used, the onMapReady method will be called.
In onCreate method, you initialize the MapView with the layout reference. You then call getMapAsync to tell the MapView that you want to use the map as soon as it's ready. When the map becomes ready, the onMapReady method is called.
In onMapReady method, you create a LatLng
object with coordinates for Sydney. Then you add a marker to the map using these coordinates. Finally, you move the camera to center it on the marker.
Use
Google Maps can be used to show location of any place or finding directions to a particular place. It is also useful in geocoding, reverse geocoding, and finding the distance between two points.
Important Points
- You need a valid Google Maps API key in order to use Google Maps in your Android app.
- Include the Google Play Services Library in your project.
- Use MapView object to add Google Maps view in the xml file.
- In the activity you need to initialize the MapView by calling onCreate method of MapView.
Summary
This article explains how to use Google Maps in an Android Studio project. The article provided code examples for initializing Maps and displaying it, along with an explanation of each step. The article also highlighted some of the features of Google Maps and provided important points to keep in mind while working with Google Maps in Android Studio.