2012-10-16 78 views
0

如果我評論onPause compass.disableonResume compass.Enable應用程序的開始和工作,但我沒有在我的谷歌地圖指南針。谷歌地圖指南針崩潰與onPause&onCreate(刪除它們的作品,但我沒有指南針)

另一方面,如果我取消註釋這兩行應用程序甚至不顯示。就像被阻塞了,那我再次評論這兩行和仿真器再工作。所以有些地方是指南針的問題,但我不知道在哪裏,有什麼幫助。

MapView map; 
MyLocationOverlay compass; 
MapController controller; 
GeoPoint touchedPoint; 
Drawable d; 
List<Overlay> overlayList; 
LocationManager lm; 
String towers; 
long start; 
long stop;  
int x, y; 
int latit; 
int longi; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_LEFT_ICON);// dodao za favicon 
    setContentView(R.layout.activity_main); 
    setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.logo); 
    map = (MapView) findViewById(R.id.mvMain); 
    map.setBuiltInZoomControls(true); 
    this.setTitle("EasyMaps=QRZ"); 

    Touchy t = new Touchy(); 
    overlayList = map.getOverlays(); 
    overlayList.add(t); 

    compass = new MyLocationOverlay(MainActivity.this, map); 
    overlayList.add(compass); 

    controller = map.getController(); 
    GeoPoint point = new GeoPoint((int) (47.975 * 1E6), (int) (17.056 * 1E6)); 
    controller.animateTo(point); 
    controller.setZoom(17); 

    d = getResources().getDrawable(R.drawable.icon); 


    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    Criteria crit = new Criteria(); 

    towers = lm.getBestProvider(crit, false); 
    Location location = lm.getLastKnownLocation(towers); 

    if (location != null) { 
     latit = (int) (location.getLatitude() * 1E6); 
     longi = (int) (location.getLongitude() * 1E6); 
     GeoPoint ourLocation = new GeoPoint(latit, longi); 
     OverlayItem overlayitem = new OverlayItem(ourLocation, "Location", "Location"); 
     CustomPinpoint custom = new CustomPinpoint(d, MainActivity.this); 
     custom.insertPinpoint(overlayitem); 
     overlayList.add(custom); 
    } else { 
     Toast.makeText(MainActivity.this, "Provider not avaiable!", Toast.LENGTH_SHORT).show(); 
    } 

} 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 

    // Checks the orientation of the screen 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show(); 
     } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { 
    Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show(); 
      } 
     } 

@Override 
protected void onResume() { 
    super.onResume(); 
    compass.enableCompass();//Problem 
    lm.requestLocationUpdates(towers, 500, 1, this); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    compass.disableCompass();//Problem 
    lm.removeUpdates(this); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

@Override 
protected boolean isRouteDisplayed() { 
    // TODO Auto-generated method stub 
    return false; 
} 

class Touchy extends Overlay { 
    public boolean onTouchEvent(MotionEvent e, MapView m) { 

     if (e.getAction() == MotionEvent.ACTION_DOWN) { 
      start = e.getEventTime(); 
      x = (int) e.getX() + 11; 
      y = (int) e.getY() - 15; 
      touchedPoint = map.getProjection().fromPixels(x, y); 

     } 
     if (e.getAction() == MotionEvent.ACTION_UP) { 
      stop = e.getEventTime(); 
     } 
     if (stop - start > 1500) { 
      // to do action 
      AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create(); 
      alert.setTitle("Option panel"); 
      alert.setMessage("Choose an option!"); 
      alert.setButton("Put flag", new DialogInterface.OnClickListener() { 

         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          // TODO Auto-generated method stub 
          OverlayItem overlayitem = new OverlayItem(touchedPoint, "Flag", "Flag"); 
          CustomPinpoint custom = new CustomPinpoint(d, MainActivity.this); 
          custom.insertPinpoint(overlayitem); 
          overlayList.add(custom); 
         } 
        }); 
      alert.setButton2("Get address", new DialogInterface.OnClickListener() { 

         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          // TODO Auto-generated method stub 
          Geocoder geocoder = new Geocoder(getBaseContext(), Locale.getDefault()); 
          try { 
           List<Address> address = geocoder.getFromLocation(touchedPoint.getLatitudeE6()/1E6, touchedPoint.getLongitudeE6()/1E6, 1); 
           if (address.size() > 0) { 
            String display = ""; 
            for (int i = 0; i < address.get(0).getMaxAddressLineIndex(); i++) { 
             display += address.get(0).getAddressLine(i) + "\n"; 
            } 
            Toast t3 = Toast.makeText(getBaseContext(), display, Toast.LENGTH_LONG); 
            t3.show(); 
           } 

          } catch (IOException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } finally { 
           // Nista 
          } 
         } 
        }); 
      alert.setButton3("View", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          // TODO Auto-generated method stub 
          if (map.isSatellite()) { 
           map.setSatellite(false); 
           map.setStreetView(true); 
          } else { 
           map.setStreetView(false); 
           map.setSatellite(true); 
          } 
         } 
        }); 
      alert.show(); 
      return true; 
     } 

     return false; 
    } 
} 

@Override 
public void onLocationChanged(Location l) { 
    // TODO Auto-generated method stub 
    latit = (int) (l.getLatitude() * 1E6); 
    longi = (int) (l.getLongitude() * 1E6); 
    GeoPoint ourLocation = new GeoPoint(latit, longi); 
    OverlayItem overlayitem = new OverlayItem(ourLocation, "Location", "LOcation"); 
    CustomPinpoint custom = new CustomPinpoint(d, MainActivity.this); 
    custom.insertPinpoint(overlayitem); 
    overlayList.add(custom); 

} 

@Override 
public void onProviderDisabled(String provider) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onProviderEnabled(String provider) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
    // TODO Auto-generated method stub 

} 

}

回答

0

您可能需要使用不同的名稱。你所說的「指南針」實際上就是你的位置,它的行爲肯定與指南針不同 - 對於初學者來說,它主要指向不同的方向。

在嘗試啓用或禁用MyLocation覆蓋圖之前,您需要檢查它是否實際上非空。

if (mMyLocationOverlay != null) 
    mMyLocationOverlay.disableMyLocation();