Adding Satellite View in Android Studio
This article will guide you through the process of integrating satellite view into your Android app using Google Maps Android API. Let’s break down the steps involved.
1. Project Setup
1.1 Add Google Maps SDK to Your Project
* Navigate to your project’s `build.gradle` file (Module: app).
* Add the following dependency:
“`
dependencies {
implementation ‘com.google.android.gms:play-services-maps:18.1.0’ // Update to the latest version if needed
// Other dependencies
}
“`
* Sync your project with Gradle files.
2. Obtain an API Key
2.1 Google Cloud Platform
* Visit the Google Cloud Platform Console: https://console.cloud.google.com.
* Create a new project or select an existing one.
* Enable the Google Maps Android API for your project.
* Obtain an API key for your project.
3. Create a Map Fragment
3.1 Design your Layout (activity_main.xml)
* Open your `activity_main.xml` file.
* Add a `fragment` element:
“`
“`
3.2 Access the Fragment in Your Activity (MainActivity.java)
* Open your `MainActivity.java`.
* Initialize the Map Fragment:
“`
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;
// …
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
// This is where you can add markers or move the camera.
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Set initial camera position
LatLng sydney = new LatLng(-34, 151);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 10));
// Enable satellite view
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
}
“`
4. Displaying Satellite View
* In your `onMapReady` method, set the map type to `GoogleMap.MAP_TYPE_SATELLITE`:
“`
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
“`
5. Example: Changing Map Type
* You can allow the user to switch between map types:
“`
// … other imports
import android.view.View;
import android.widget.Button;
// …
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
// … existing code
Button btnNormal, btnSatellite, btnTerrain, btnHybrid;
@Override
public void onCreate(Bundle savedInstanceState) {
// … existing code
btnNormal = findViewById(R.id.btnNormal);
btnSatellite = findViewById(R.id.btnSatellite);
btnTerrain = findViewById(R.id.btnTerrain);
btnHybrid = findViewById(R.id.btnHybrid);
// Set click listeners for buttons
btnNormal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
});
btnSatellite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
});
btnTerrain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
}
});
btnHybrid.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}
});
// … rest of onCreate code
}
// … existing code
}
“`
6. Map Types Comparison
| Map Type | Description |
|—|—|
| `GoogleMap.MAP_TYPE_NORMAL` | Standard map with roads, labels, and points of interest. |
| `GoogleMap.MAP_TYPE_SATELLITE` | Satellite imagery of the Earth’s surface. |
| `GoogleMap.MAP_TYPE_TERRAIN` | Terrain-based map with shaded relief, highlighting mountains and valleys. |
| `GoogleMap.MAP_TYPE_HYBRID` | Hybrid map with satellite imagery overlaid with roads and labels. |