簡體   English   中英

如何在Android中獲取經度和緯度並將其傳遞給canvas類,以便我可以在位圖上繪制

[英]How to get longitude and latitude in android and pass it on to a canvas class so i can draw on a bitmap

最近3個月我一直在做android編程,但是在這個問題上我很難受。

我的任務:我希望設備能夠獲取其當前位置(緯度和經度),並將其傳遞給我有位圖的另一個類。 位圖是我自己繪制的地圖。 那么,有人知道將緯度和經度坐標傳遞給另一個類的簡單方法嗎?

注意:一旦獲得坐標並將其傳遞給另一個類,我就會知道如何在位圖上進行繪制。 我正在使用eclipse和java btw。 我希望不使用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

    }


    }

從GPS接收器中,您可以獲得一個Location object 它實現了Parcelable -interface。

如果要將此對象傳遞給另一個Activity,可以將其添加到Bundle

Intent i = new Intent(...);
i.putExtra("Location", location); // The object.
startActivity(i);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM