0

過去3か月間Androidプログラミングを行っていますが、この問題に苦労しています。

私の仕事: デバイスが現在の位置 (緯度と経度) を取得し、それをビットマップを持つ別のクラスに渡すことができるようにします。ビットマップは、私が自分で描いたマップです。では、緯度と経度の座標を別のクラスに渡す簡単な方法を知っている人はいますか?

注:座標を取得して他のクラスに渡すと、ビットマップに描画する方法がわかりました。私は日食とJavaところで働いています。そして、phonegapを使わずにこれをやりたいです。

編集:次のコードを使用しました(チュートリアルで見つけた場所)。使用すると、場所を表示できます。問題は、彼が正確に場所を取得する方法がわからないことです。このコードを使用して場所を別のクラスに渡す方法を教えてください。

よろしくお願いします!

package org.mh00217.SilineSnap;


public class LocationActivity extends MapActivity implements LocationListener { //<1>

      private static final String TAG = "LocationActivity";



      LocationManager locationManager; //<2>
      Geocoder geocoder; //<3>
      TextView locationText;
      MapView map;  
      MapController mapController; //<4>

    @Override
      public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.location);

        locationText = (TextView)this.findViewById(R.id.lblLocationInfo);
        map = (MapView)this.findViewById(R.id.mapview);
        map.setBuiltInZoomControls(true);

        mapController = map.getController(); //<4>
        mapController.setZoom(16);

        locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE); //<2>

        geocoder = new Geocoder(this); //<3>

        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); //<5>
        if (location != null) {
          Log.d(TAG, location.toString());
          this.onLocationChanged(location); //<6>

        }
      }

      @Override
      protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this); //<7>
      }

      @Override
      protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this); //<8>
      }

      @Override
      public void onLocationChanged(Location location) { //<9>

        Log.d(TAG, "onLocationChanged with location " + location.toString());
        String text = String.format("Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f", location.getLatitude(), 
                      location.getLongitude(), location.getAltitude(), location.getBearing());
        this.locationText.setText(text);

        try {
          List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10); //<10>
          for (Address address : addresses) {
            this.locationText.append("\n" + address.getAddressLine(0));
          }

          int latitude = (int)(location.getLatitude() * 1000000);
          int longitude = (int)(location.getLongitude() * 1000000);

          GeoPoint point = new GeoPoint(latitude,longitude);
          mapController.animateTo(point); //<11>

        } catch (IOException e) {
          Log.e("LocateMe", "Could not get Geocoder data", e);
        }
      }

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

    @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

GPS 受信機からLocation-objectを取得します。インターフェースを実装していParcelableます。

このオブジェクトを別のアクティビティに渡したい場合は、次のように追加できますBundle:

Intent i = new Intent(...);
i.putExtra("Location", location); // The object.
startActivity(i);
于 2013-04-07T11:29:21.647 に答える