Android谷歌地图在卫星视图中显示灰色瓷砖

时间:2016-11-19 06:52:26

标签: android google-maps

我正在尝试使用Google Maps API来显示" normal"查看和卫星视图。

我的应用可以在普通视图中显示任何位置,但在将视图更改为卫星视图时仅显示灰色图块。

普通视图

Normal view on the app

卫星视图

enter image description here

这是我目前用于在视图之间切换的代码:

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

我做错了什么? (编辑显示完整活动)

2 个答案:

答案 0 :(得分:1)

MAP_TYPE_ITEMS和开关的顺序不同。当您点击Satellite时,它会显示Terrain try this link

答案 1 :(得分:1)

检查并尝试此

在设置地图类型

后添加此项
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();
            }
        });

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