Set Map Type For Google Maps Activeness Using Google Maps Android Api V2

Posted on

Further industrial plant on the instance “Display “Legal Notices” for Google Maps Android API v2 on Options Menu“, to add together selection for users to direct Map Type.


To ready map type, telephone telephone the setMapType() method og GoogleMap object.

Modify from the example “Display “Legal Notices” for Google Maps Android API v2 on Options Menu“:

Edit menu/activity_main.xml to add together options for map type

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto">     <item         android:id="@+id/menu_about"         android:orderInCategory="100"         app:showAsAction="ifRoom"         android:title="Set map type for Google Maps Activity using Google Maps Android API v2"/>     <item         android:id="@+id/maptype"         android:orderInCategory="100"         app:showAsAction="never"         android:title="Set map type for Google Maps Activity using Google Maps Android API v2">         <menu >             <group android:id="@+id/groupmaptype"                 android:checkableBehavior="single">                 <item android:id="@+id/maptypeNORMAL"                     android:title="Set map type for Google Maps Activity using Google Maps Android API v2" />                 <item android:id="@+id/maptypeSATELLITE"                     android:title="Set map type for Google Maps Activity using Google Maps Android API v2" />                 <item android:id="@+id/maptypeTERRAIN"                     android:title="Set map type for Google Maps Activity using Google Maps Android API v2" />                 <item android:id="@+id/maptypeHYBRID"                     android:title="Set map type for Google Maps Activity using Google Maps Android API v2" />                 <item android:id="@+id/maptypeNONE"                     android:title="Set map type for Google Maps Activity using Google Maps Android API v2" />              </group>         </menu>      </item>     <item         android:id="@+id/menu_legalnotices"         android:orderInCategory="100"         app:showAsAction="never"         android:title="Set map type for Google Maps Activity using Google Maps Android API v2"/>   </menu> 

Edit MapsActivity.java to telephone telephone mMap.setMapType() 1 time users selected.

package com.blogspot.android_er.androidstudiomapapp;          import android.app.AlertDialog;         import android.content.DialogInterface;         import android.content.Intent;         import android.net.Uri;         import android.os.Bundle;         import android.support.v7.app.AppCompatActivity;         import android.view.Menu;         import android.view.MenuItem;          import com.google.android.gms.common.GoogleApiAvailability;         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;  world story MapsActivity extends AppCompatActivity implements OnMapReadyCallback {      mortal GoogleMap mMap;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_maps);         // Obtain the SupportMapFragment in addition to teach notified when the map is produce to move used.         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()                 .findFragmentById(R.id.map);         mapFragment.getMapAsync(this);      }      /**      * Manipulates the map 1 time available.      * This callback is triggered when the map is produce to move used.      * This is where nosotros tin add together markers or lines, add together listeners or motion the camera. In this case,      * nosotros only add together a marking close Sydney, Australia.      * If Google Play services is non installed on the device, the user volition move prompted to install      * it within the SupportMapFragment. This method volition solely move triggered 1 time the user has      * installed Google Play services in addition to returned to the app.      */     @Override     world void onMapReady(GoogleMap googleMap) {         mMap = googleMap;          // Add a marking inwards Sydney in addition to motion the photographic idiot box camera         LatLng sydney = novel LatLng(-34, 151);         mMap.addMarker(new MarkerOptions().position(sydney).title("Marker inwards Sydney"));         mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));     }      @Override     world boolean onCreateOptionsMenu(Menu menu) {         getMenuInflater().inflate(R.menu.activity_main, menu);         supply super.onCreateOptionsMenu(menu);     }      @Override     world boolean onOptionsItemSelected(MenuItem item) {         switch (item.getItemId()) {             instance R.id.maptypeHYBRID:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);                     supply true;                 }             instance R.id.maptypeNONE:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_NONE);                     supply true;                 }             instance R.id.maptypeNORMAL:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);                     supply true;                 }             instance R.id.maptypeSATELLITE:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);                     supply true;                 }             instance R.id.maptypeTERRAIN:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);                     supply true;                 }             instance R.id.menu_legalnotices:                 String LicenseInfo = GoogleApiAvailability                         .getInstance()                         .getOpenSourceSoftwareLicenseInfo(MapsActivity.this);                 AlertDialog.Builder LicenseDialog =                         novel AlertDialog.Builder(MapsActivity.this);                 LicenseDialog.setTitle("Legal Notices");                 LicenseDialog.setMessage(LicenseInfo);                 LicenseDialog.show();                 supply true;             instance R.id.menu_about:                 AlertDialog.Builder aboutDialogBuilder =                         novel AlertDialog.Builder(MapsActivity.this);                 aboutDialogBuilder.setTitle("About Me")                         .setMessage("https://rootandroid.org/");                  aboutDialogBuilder.setPositiveButton("visit",                         novel DialogInterface.OnClickListener() {                     @Override                     world void onClick(DialogInterface dialog, int which) {                         String url = "https://rootandroid.org/";                         Intent i = novel Intent(Intent.ACTION_VIEW);                         i.setData(Uri.parse(url));                         startActivity(i);                     }                 });                  aboutDialogBuilder.setNegativeButton("Dismiss",                         novel DialogInterface.OnClickListener() {                     @Override                     world void onClick(DialogInterface dialog, int which) {                         dialog.cancel();                     }                 });                  AlertDialog aboutDialog = aboutDialogBuilder.create();                 aboutDialog.show();                  supply true;         }         supply super.onOptionsItemSelected(item);     } }  


Step-by-step of Android Google Maps Activity using Google Maps Android API v2, on Android Studio.