Android 尝试获取位置时出现异常
我想获取当前位置经度和经度,但代码中有一些错误 一,。地图视图、活动主电视位置、当前位置无法解析或不是字段 这是我的密码:Android 尝试获取位置时出现异常,android,google-maps,gps,Android,Google Maps,Gps,我想获取当前位置经度和经度,但代码中有一些错误 一,。地图视图、活动主电视位置、当前位置无法解析或不是字段 这是我的密码: public class MainActivity extends MapActivity implements LocationListener { private MapView mapView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedI
public class MainActivity extends MapActivity implements LocationListener {
private MapView mapView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting reference to MapView
mapView = (MapView) findViewById(R.id.map_view );
// Setting Zoom Controls on MapView
mapView.setBuiltInZoomControls(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, 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;
}
@Override
public void onLocationChanged(Location location) {
TextView tvLocation = (TextView) findViewById(R.id.tv_location);
// Getting latitude
double latitude = location.getLatitude();
// Getting longitude
double longitude = location.getLongitude();
// Setting latitude and longitude in the TextView tv_location
tvLocation.setText("Latitude:" + latitude + ", Longitude:"+ longitude );
// Creating an instance of GeoPoint corresponding to latitude and longitude
GeoPoint point = new GeoPoint((int)(latitude * 1E6), (int)(longitude*1E6));
// Getting MapController
MapController mapController = mapView.getController();
// Locating the Geographical point in the Map
mapController.animateTo(point);
// Applying a zoom
mapController.setZoom(15);
// Redraw the map
mapView.invalidate();
// Getting list of overlays available in the map
List<Overlay> mapOverlays = mapView.getOverlays();
// Creating a drawable object to represent the image of mark in the map
Drawable drawable = this.getResources().getDrawable(R.drawable.cur_position);
// Creating an instance of ItemizedOverlay to mark the current location in the map
CurrentLocationOverlay currentLocationOverlay = new CurrentLocationOverlay(drawable);
// Creating an item to represent a mark in the overlay
OverlayItem currentLocation = new OverlayItem(point, "Current Location", "Latitude : " + latitude + ", Longitude:" + longitude);
// Adding the mark to the overlay
currentLocationOverlay.addOverlay(currentLocation);
// Clear Existing overlays in the map
mapOverlays.clear();
// Adding new overlay to map overlay
mapOverlays.add(currentLocationOverlay);
}
@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
}
}您似乎已抓取此代码并将其粘贴到项目的活动中。但是,您似乎没有添加其他资源。如果这是从一个网站,张贴一个链接到该网站的OP
例如,考虑OP中提到的MavaVIEW中的错误,引用这行代码:
mapView=mapView findViewByIdR.id.map\U视图 cur_位置的误差指的是这一行: Drawable Drawable=this.getResources.getDrawableR.Drawable.cur\u位置 验证布局XML活动\u main中是否存在id为map\u view的MapView。至于可绘制cur_位置,请确保您在项目中的任何一个可绘制文件夹中都有我假设的图像资源另一个activity\u maintv\u位置没有出现在上面发布的代码中,但是上面的任何一个建议都会解决这个问题。试试这个,你的activity必须像implements LocationListener这样实现{
发布布局xml文件。。
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
lat = (double) (location.getLatitude());
lng = (double) (location.getLongitude());
Log.i(TAG, "Lattitude:" + lat);
Log.i(TAG, "Longitude:" + lng);
Toast.makeText( this, "Current location:\nLatitude: " + lat + "\n" + "Longitude: " + lng, Toast.LENGTH_LONG).show();
// create geo point
GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
MapController controller = mapview.getController();
controller.animateTo(point);
controller.setZoom(13);
mapview.setBuiltInZoomControls(true);
drawable = getResources().getDrawable(R.drawable.marker);
OverlayItem overlayItem = new OverlayItem(point, "", "");
itemizedOverlay = new SimpleItemizedOverlay(drawable, mapview);
itemizedOverlay.addOverlay(overlayItem);
mapOverlays = mapview.getOverlays();
mapview.getOverlays().add(itemizedOverlay);
mapview.invalidate();
} else {
System.out.println("Location not avilable");
}
locationManager.removeUpdates(this);
}