1

我想获取当前位置(经度和经度),但我在代码中遇到了一些错误。

1. map_view, activity_maintv_location,cur_position无法解析或不是字段。

这是我的代码:

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
}

}

4

2 回答 2

0

您似乎已获取此代码并将其粘贴到项目的活动中。但是,您似乎没有添加其他资源。如果这是来自网站,请在 OP 中发布指向该网站的链接。

例如,考虑map_viewOP 中所述的错误是指这行代码:

mapView = (MapView) findViewById(R.id.map_view);

错误是cur_position指这一行:

Drawable drawable = this.getResources().getDrawable(R.drawable.cur_position);

验证您的布局 XML 中是否存在MapViewid 的a 。至于,请确保您在项目的任何一个文件夹中都有图像资源(我假设) 。map_viewactivity_mainDrawable cur_positionDrawable

另一个activity_maintv_location没有出现在上面发布的代码中,但是上面的任何一个建议都可以解决这个问题。

于 2013-03-16T06:50:40.653 回答
0

试试这个,你的 Activity 必须是这样的实现implements LocationListener {

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);
}
于 2013-03-16T07:43:28.610 回答