Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Map Type in Android Map App

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);
    }

}
like image 342
Max Cushner Avatar asked Mar 30 '16 03:03

Max Cushner


Video Answer


2 Answers

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);
like image 152
Arpit Patel Avatar answered Oct 16 '22 18:10

Arpit Patel


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);
like image 9
Sagar Nayak Avatar answered Oct 16 '22 19:10

Sagar Nayak