google_maps_api.xml
The XML File
res/layout/YOUR_ACTIVITY_NAME.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
activity.xml
file in future tutorials.The Maps Activity Java File
java/com.xxx.xxx/YOUR_ACTIVITY_NAME.java
. This file contains the code that will run when you start your Google Maps Application. By default, your activity.java
file should look something like this:import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney, Australia, 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));
}
}
Android Manifest File
manifests/AndroidManifest.xml
. This file contains all the permissions, details, libraries, and specifications about your application. The android manifest is vital to your program as it is needed for any application before it can open and run successfully.Throughout the construction of your Google Maps Android Application, you will be mainly modifying these three files.
As of right now, you are probably going to need a physical android device to run your Google Maps Application since android virtual device does not have location services capability. Make sure developer tools is enabled on your android device. Then, plug in your android device and click the green play button at the top of android studio. This compiles your code and installs the apk (Android Package Kit) into your android device. Run the app to make sure it's working. By default, it should be showing a simple map with which you can zoom and move round.
In the next tutorial, I will be showing you how to play with markers as well as use the location services of your device and integrate into your application. Stay tuned!