Monday, January 21, 2013

How to change between satellite and traffic layers in Google Maps on Android

I wanted to open up a dialog box from the Google Maps View to toggle between the satellite and traffic map layers as shown in the screenshot below.

I used the code snippet below to toggle between the map layers. But it could not toggle between the layers.
 @Override  
 public void onClick(DialogInterface dialog, int which) {  
 // TODO Auto-generated method stub  
 String layer = (String) layers[which];  
 if (layer.compareToIgnoreCase("traffic")==0){  
 _mapView.setTraffic(true);  
 _mapView.invalidate();  
 }  
 else if (layer.compareToIgnoreCase("satellite")==0) {   
 _mapView.setSatellite(true);  
 _mapView.invalidate();  
 }  
 dialog.dismiss();  
 }  

Later, I found out I could resolve the problem by setting the previous layer to false as shown in the snippet below.

 @Override  
 public void onClick(DialogInterface dialog, int which) {  
 // TODO Auto-generated method stub  
 String layer = (String) layers[which];  
 if (layer.compareToIgnoreCase("traffic")==0){  
 _mapView.setSatellite(false);  
 _mapView.setTraffic(true);  
 _mapView.invalidate();  
 }  
 else if (layer.compareToIgnoreCase("satellite")==0) {  
 _mapView.setTraffic(false);  
 _mapView.setSatellite(true);  
 _mapView.invalidate();  
 }  
 dialog.dismiss();  
 }  

No comments:

Related Posts with Thumbnails