Rana Talha Tariq Rana Talha Tariq - 1 month ago 17
Android Question

Satellite Map Type not working

Respected members, I am working on a basic Google Maps application. I have implemented some functionalities like GPS check, Internet check, working internet check etc for this app. I have just added an options menu at top with one single SATELLITE TYPE map. I have WRITTEN the correct code but whenever I click on SATELLITE option to change my map view, the app CLOSES itself. There is no error being shown in the terminal. App is working fine for all the other things. I can't figure out where I'm doing wrong which is closing my app rather than loading a specific map type.
Following is my main_menu XML resource file:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/SatelliteType"
android:title="@string/satellite"/>
</menu>


Following is the Java code. Please let me know if I need to elaborate the things more clearly to understand my mistake / shortcomings in this coding practice:

package com.example.Test;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
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;
import java.net.InetAddress;

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {
GoogleMap googleMap;
private static final int ERROR_DIALOG_REQUEST = 9901;

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);


//****************************Checking the GPS at start*******************
final LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

Toast.makeText(this, "GPS is Enabled in your device", Toast.LENGTH_SHORT).show();
} else {
showGPSDisabledAlertToUser();
}

//********Checking the Google Play Services at start*********
if (ServicesOK()) {
Toast.makeText(this, "CONNECTED to Google Play Services", Toast.LENGTH_SHORT).show();
}
//****************************Checking the NETWORKING DEVICE at start*******************
if (isConnected()) {
Toast.makeText(this, "CONNECTED to NETWORKING DEVICE", Toast.LENGTH_SHORT).show();
}
//****************************Checking the INTERNET SERVICE at Start*******************
if (isInternetAvailable()) {
Toast.makeText(this, "CONNECTED to INTERNET", Toast.LENGTH_SHORT).show();
}

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

}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//****************************When MAP is perfectly LOADED*******************
@Override
public void onMapReady(GoogleMap googleMap) {
// Add a marker in CIITLAHORE and move the camera
LatLng ciitlahore = new LatLng(31.400693, 74.210941);
googleMap.addMarker(new MarkerOptions().position(ciitlahore).title("Marker in CIIT LAHORE"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(ciitlahore));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(17.0f));
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//****************************Making MENU Option******************

@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_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//Add menu handling code
switch (id) {
case R.id.SatelliteType:
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
return (true);
default :
return super.onOptionsItemSelected(item);
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//****************************METHOD for checking GOOGLE PLAY SERVICES*************************
public boolean ServicesOK() {
int resultCode = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);

if (resultCode == ConnectionResult.SUCCESS) {
return true;
} else {
GoogleApiAvailability.getInstance().getErrorDialog(this, resultCode, ERROR_DIALOG_REQUEST).show();
Toast.makeText(this, "Can't connect to mapping services", Toast.LENGTH_SHORT).show();
}
return false;
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//****************************METHOD for checking INTERNET AVAILABLE*************************
public boolean isInternetAvailable() {
try {
InetAddress ipAddr = InetAddress.getByName("google.com");
return !ipAddr.equals("");
} catch (Exception e) {
return false;
}
}

//****************************METHOD for checking INTERNET CONNECTIVITY*************************

public boolean isConnected() {
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) {
// connected to the internet
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
// connected to wifi
Toast.makeText(this, "WIFI CONNECTION AVAILABLE", Toast.LENGTH_SHORT).show();
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
// connected to the mobile provider's data plan
Toast.makeText(this, "CELLULAR CONNECTION AVAILABLE", Toast.LENGTH_SHORT).show();
}
} else {
// not connected to the internet
Toast.makeText(this, "NOT CONNECTED to any working INTERNET service", Toast.LENGTH_SHORT).show();
}
return false;
}

//****************************GPS DIALOGUE BOX*************************
private void showGPSDisabledAlertToUser() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("GPS is disabled in your device. Would you like to enable it?")
.setCancelable(false)
.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(callGPSSettingIntent);
}
});

alertDialogBuilder.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
finishAffinity();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//+++++++++++++++++++++++++++++++++++++++++++ END +++++++++++++++++++++++++++++++++++++++++++++++
}

Answer

Your googleMap object is null. You need to add this to your onMapReady method:

this.googleMap = googleMap;

Also, your googleMap object may be null when you select your menu, so you will nedd to check it:

if (googleMap != null) {
    googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
Comments