[英]How to get longitude and latitude in android and pass it on to a canvas class so i can draw on a bitmap
I have been doing android programing for the last 3 months but I'm having a hard time on this problem. 最近3个月我一直在做android编程,但是在这个问题上我很难受。
My task: I want my device to be able to get its current location (latitude and longitude) and pass it to another class where I have a bitmap. 我的任务:我希望设备能够获取其当前位置(纬度和经度),并将其传递给我有位图的另一个类。 The bitmap is a Map I drew myself.
位图是我自己绘制的地图。 So does anyone know a simple way to pass latitude and longitude coordinates to another class?
那么,有人知道将纬度和经度坐标传递给另一个类的简单方法吗?
Note: once I get the coordinates and pass them to the other class I know how to draw on my bitmap. 注意:一旦获得坐标并将其传递给另一个类,我就会知道如何在位图上进行绘制。 I'm working with eclipse and java btw.
我正在使用eclipse和java btw。 And I wish to do this without using phonegap.
我希望不使用phonegap来执行此操作。
EDIT : I have used the following code (where i found it in a tutorial) and when i use it i can view the location. 编辑:我已经使用以下代码(在教程中找到的位置),当我使用它时,我可以查看位置。 The problem is i do not know how he gets the location exactly.
问题是我不知道他是怎么得到确切位置的。 So can anybody pls explain to me how can i use this code to pass the location to another class?
因此,有人可以向我解释如何使用此代码将位置传递给另一个类吗?
Many thanks in advance! 提前谢谢了!
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
}
}
From the GPS receiver, you get a Location
-object . 从GPS接收器中,您可以获得一个
Location
object 。 It implements the Parcelable
-interface. 它实现了
Parcelable
-interface。
If you want to pass this object to another Activity, you can add it to a Bundle
: 如果要将此对象传递给另一个Activity,可以将其添加到
Bundle
:
Intent i = new Intent(...);
i.putExtra("Location", location); // The object.
startActivity(i);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.