Vanessa Tan
Vanessa Tan

Reputation: 97

Android Google Map Shows Grey Tiles in Satellite View

I am trying to use the Google Maps API to show both "normal" view and satellite view.

My app can show any location in normal view but displays only grey tiles when I change the view to satellite view.

Normal View

Normal view on the app

Satellite View

enter image description here

This is the code that I'm currently using to switch between the views:

public class MapActivity extends BaseActivity implements OnMapReadyCallback {

    Geocoder geocoder;
    GoogleMap mMap;
    private static final int ERROR_DIALOGUE_REQUEST = 9001;
    private static final CharSequence[] MAP_TYPE_ITEMS = {"Road Map", "Hybrid", "Satellite", "Terrain"};


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

        //initialize geocoder
        geocoder = new Geocoder(getApplicationContext());

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.search_menu,menu);
        MenuItem searchViewItem = menu.findItem(R.id.search_bar);
        final SearchView searchViewAndroidActionBar = (SearchView) MenuItemCompat.getActionView(searchViewItem);
        searchViewAndroidActionBar.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
            @Override
            public boolean onQueryTextSubmit(String query){

                try {
                    List<Address> location = geocoder.getFromLocationName(query, 1);
                    double lat = Double.parseDouble(String.valueOf(location.get(0).getLatitude()));
                    double lon = Double.parseDouble(String.valueOf(location.get(0).getLongitude()));

                    LatLng newLocation = new LatLng(lat,lon);

                    mMap.clear();
                    mMap.addMarker(new MarkerOptions().position(newLocation));
                    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(newLocation,15));
                } catch (Exception e){
                    e.printStackTrace();
                }

                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText){
                return false;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        int id = item.getItemId();

        switch (id){
            case R.id.changeMapType:
                showMapTypeSelectorDialog();
                break;
        }
        return true;
    }

    private void showMapTypeSelectorDialog(){
        //setting up the builder
        final String title = "Select Map Type";
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title);

        //check current map type
        int checkCurrentMapType = mMap.getMapType() - 1;

        //add click listener to dialog
        builder.setSingleChoiceItems(MAP_TYPE_ITEMS,checkCurrentMapType,new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item){
                switch(item){
                    case 1:
                        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                        break;
                    case 2:
                        mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
                        break;
                    case 3:
                        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                        break;
                    default:
                        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                }
                dialog.dismiss();
            }
        });

        //build dialog
        AlertDialog fMapTypeDialogue = builder.create();
        fMapTypeDialogue.setCanceledOnTouchOutside(true);
        fMapTypeDialogue.show();
    }

    public boolean servicesOK() {

        int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

        if (isAvailable == ConnectionResult.SUCCESS){
            return true;
        } else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)){
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, ERROR_DIALOGUE_REQUEST);
            dialog.show();
        } else {
            Toast.makeText(this, "Can't connect to mapping service", Toast.LENGTH_SHORT).show();
        }

        return false;
    }

    @Override
    public void onMapReady(GoogleMap map){
        mMap = map;

        LatLng initialLocation = new LatLng(1.366898,103.814047);
        mMap.addMarker(new MarkerOptions().position(initialLocation));
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(initialLocation,10));
    }
}

What am I doing wrong? (Edited to show full activity)

Upvotes: 0

Views: 1202

Answers (2)

Quick learner
Quick learner

Reputation: 11457

Check and try this

add this after setting map type

builder.setSingleChoiceItems(MAP_TYPE_ITEMS,checkCurrentMapType,new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item){
                switch(item){
                    case 1:
                        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                        mMap.invalidate();
                        break;
                    case 2:
                        mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
                        mMap.invalidate();
                        break;
                    case 3:
                        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                         mMap.invalidate(); 
                        break;
                    default:
                        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                        mMap.invalidate();
                }
                dialog.dismiss();
            }
        });

And also Set default map type in onMapReady

 @Override
    public void onMapReady(GoogleMap map){
        mMap = map;
        // set default map here
         mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
         mMap.invalidate();
        LatLng initialLocation = new LatLng(1.366898,103.814047);
        mMap.addMarker(new MarkerOptions().position(initialLocation));
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(initialLocation,10));
    }

Upvotes: 1

Arpan Sharma
Arpan Sharma

Reputation: 2162

The order for MAP_TYPE_ITEMS and the switch are different.When you click on Satellite,it is showing Terrain try this link

Upvotes: 1

Related Questions