2013-03-16 127 views
1

我想獲取當前位置(經度和經度),但在代碼中出現一些錯誤。嘗試獲取位置時出現異常

1. map_viewactivity_maintv_locationcur_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 
} 

}

+0

後農具佈局的XML文件.. – 2013-03-16 06:46:34

+0

發表您的錯誤日誌 – MuraliGanesan 2013-03-16 06:46:36

回答

0

你似乎已經抓住了這個代碼,並在項目的活動貼吧。但是,您似乎沒有添加其他資源。如果這是來自網站,請在OP中張貼該網站的鏈接。

例如,考慮map_view錯誤作爲OP說指的是這行代碼:

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

而對於cur_position錯誤指的是這一行:

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

驗證您的佈局中是否存在編號爲map_viewMapView XML activity_main。至於Drawable cur_position,請確保您在項目的Drawable文件夾中有圖像資源(我假設)。

另一個activity_maintv_location沒有出現在上面的代碼中,但是上面的任何一個建議都可以解決這個問題。

0

試試這個,你的活動必須是這樣的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); 
} 
相關問題