I'm very new to programming. I'm taking a mobile development class and this is all new to me. I have created a very basic Android app in Android studio that shows a standard Google Map. I need to create buttons on the app to change the map from normal, to satellite, terrain, and hybrid. I've been searching around here and on multiple other sources, but I don't know where to begin, or where to put the code I find on other sites. Can anyone help? I'm sure this is fairly easy, I just don't know what to do.
Thanks!
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MapActivity"
tools:ignore="MissingPrefix">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="5dp"
class="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
map:cameraBearing="112.5"
map:cameraTargetLat="-33.796923"
map:cameraTargetLng="150.922433"
map:cameraTilt="30"
map:cameraZoom="13"
map:uiCompass="true"
map:uiRotateGestures="true"
map:uiScrollGestures="false"
map:uiTiltGestures="true"
map:uiZoomControls="true"
map:uiZoomGestures="true"
/>
</RelativeLayout>
.java file
package geog498v.mymap;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapActivity extends FragmentActivity implements OnMapReadyCallback {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_map, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.getUiSettings().setMapToolbarEnabled(true);
googleMap.getUiSettings().setZoomGesturesEnabled(true);
googleMap.getUiSettings().setScrollGesturesEnabled(true);
googleMap.getUiSettings().setTiltGesturesEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(true);
}
}
You can use option menu for that
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_MyLocation:
//startActivity(new Intent(this, MyLocation.class));
return(true);
case MENU_LocationCar:
startActivity(new Intent(this, Gps.class));
return(true);
case MENU_Satellite:
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
map.invalidate();
return(true);
case MENU_Terrain:
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
map.invalidate();
return(true);
}
return(super.onOptionsItemSelected(item));
}
you can also add more
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
to change map type , use this inside your onMapReady() .
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With