2016-11-19 75 views
0

我嘗試使用谷歌地圖API來顯示「正常」視圖和衛星視圖。谷歌Android地圖顯示灰瓦衛星視圖

我的應用程序可以顯示在普通視圖中的任何位置,但是當我改變了看法衛星視圖顯示只有青瓦。

普通視圖

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

我在做什麼錯? (編輯到顯示完整的活動)

+0

發表您的完整活動或片段類 –

+0

張貼了我的滿地圖活動 –

+0

是U使用虛擬設備或真實設備? –

回答

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

我得到的錯誤'無法解析法「無效()」'當我嘗試這 –

1

順序爲MAP_TYPE_ITEMS和交換機different.When你點擊衛星,它顯示地形 try this link

+0

感謝您的幫助,注意到錯誤! –

+0

你welcome.Enjoy編碼 –