0

コメントするonPause compass.disableと、onResume compass.Enableアプリは起動して動作しますが、Google マップにコンパスがありません。

一方、これらの 2 行のコメントを外すと、アプリは表示されません。Like がブロックされた後、この 2 行を再度コメントすると、エミュレーターが再び機能します。コンパスのどこかに問題がありますが、どこにあるのかわかりません。

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

}

}

4

1 に答える 1

0

異なる名前を使用したい場合があります。あなたが「コンパス」と呼んでいるものは、実際にはあなたの場所であり、コンパスとは確かに異なる動作をします - まず第一に、それはほとんど別の方向を指します.

MyLocation オーバーレイを有効または無効にする前に、それが実際に null でないかどうかを確認する必要があります。

    if (mMyLocationOverlay != null)
    mMyLocationOverlay.disableMyLocation();
于 2013-01-07T15:53:15.847 に答える