2

我想在一项活动中获取我的位置,然后将其发送到另一个活动(当我单击按钮时),然后在地图上显示我的位置。这是我的代码(问题是,地图没有动画到我当前的位置,我相信第一个活动没有获得当前位置并将 null 发送到第二个活动):

public class ActivityMain extends MapActivity {



private LocationManager locationManager;
private LocationListener locationListener;
private GeoPoint currentGeoPoint = null;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activitymain);

    try{
         locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
         locationListener = new getLocation();
         locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);


    }
    catch (NullPointerException e){
        System.out.println("Null");
        e.printStackTrace();
    }
    catch (Exception e) {
        e.printStackTrace();
    }   
}

public void onButton1Click(View view) {

    Intent intent = new Intent(ActivityMain.this, ActivityMap.class);
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

     if(currentGeoPoint != null){
         intent.putExtra("lat", currentGeoPoint.getLatitudeE6());
         intent.putExtra("long", currentGeoPoint.getLongitudeE6());
     }
     else{

         try{
             locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
             locationListener = new getLocation();
             locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);


        }
        catch (NullPointerException e){
            System.out.println("Null");
            e.printStackTrace();
        }
        catch (Exception e) {
            e.printStackTrace();
        }   
     }

     Context c = getApplicationContext();
     c.startActivity(intent);

     //Intent intent = new Intent(ActivityMain.this, ActivityMap.class);
    //this.startActivity(intent);

}


public void onButton2Clik (View view){
    Intent intent = new Intent(ActivityMain.this, ActivityListCategories.class);
    this.startActivity(intent);

    } 

class getLocation implements LocationListener {


    public void onLocationChanged(Location location) {
        if (location != null){
        GeoPoint currentPoint = getCurrentPoint(location);
        currentGeoPoint = currentPoint; 

        }

    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    public void onProviderEnabled(String provider) {
        Toast.makeText(getApplicationContext(), getProvider() + " enabled", Toast.LENGTH_SHORT).show();

    }

    public void onProviderDisabled(String provider) {
        Toast.makeText(getApplicationContext(), getProvider() + " disabled", Toast.LENGTH_SHORT).show();            
    }


}



public GeoPoint getLastKnownPoint (){

    GeoPoint lastKnownPoint = null;
    Location lastKnownLocation = locationManager.getLastKnownLocation(getProvider());
    if(lastKnownLocation != null){
         lastKnownPoint = getCurrentPoint(lastKnownLocation);
    }

    return lastKnownPoint;
}


public GeoPoint getCurrentPoint (Location location){
    GeoPoint currentPoint = new GeoPoint((int)(location.getLatitude()*1E6),(int)(location.getLongitude()*1E6));
    return currentPoint;

}

public String getProvider() {
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
    criteria.setAccuracy(Criteria.NO_REQUIREMENT);
    String Provider = locationManager.getBestProvider(criteria, true);
    return Provider;
}



@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

protected void onResume(){
    super.onResume();
    LocationManager newLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager = newLocationManager;
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);


}

protected void onPause(){
    super.onPause();
    locationManager.removeUpdates(locationListener);


}

protected void onDestroy(){
    super.onDestroy();
    locationManager.removeUpdates(locationListener);


}

}

这是第二个活动的代码

public class ActivityMap extends MapActivity {

private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
private LocationListener locationListener;
private GeoPoint currentGeoPoint;
private Location currentLocation = null;
private ClassMapOverlay currPos;
private ClassCustomItemizedOverlay<ClassCustomOverlayItem> mallsOverlay;
private List<ClassMall> malls;



// TODO: AsyncTAsk

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activitymap);

        Bundle bundle = getIntent().getExtras();

        if(bundle != null){
            currentGeoPoint = new GeoPoint((int)(bundle.getDouble("lat") / 1E6),(int)(bundle.getDouble("long")/1E6));
            animateToCurrentPoint(currentGeoPoint);

        }

        mapView = (MapView) findViewById(R.id.mapView);
        mapController = mapView.getController();


    public GeoPoint getLastKnownPoint (){

        GeoPoint lastKnownPoint = null;
        Location lastKnownLocation = locationManager.getLastKnownLocation(getProvider());
        if(lastKnownLocation != null){
             lastKnownPoint = getCurrentPoint(lastKnownLocation);
        }

        return lastKnownPoint;
    }


    public GeoPoint getCurrentPoint (Location location){
        GeoPoint currentPoint = new GeoPoint((int)(location.getLatitude()*1E6),(int)(location.getLongitude()*1E6));
        return currentPoint;

    }

    public void animateToCurrentPoint(GeoPoint currentPoint){

        mapController.animateTo(currentPoint);
        mapController.setCenter(currentPoint);
        mapController.setZoom(15);
    }


    public void drawCurrPositionOverlay(){
        List<Overlay> overlays = mapView.getOverlays();
        overlays.remove(currPos);
        Drawable marker = getResources().getDrawable(R.drawable.me);
        currPos = new ClassMapOverlay(marker,mapView);
        GeoPoint drawMyPoint = null;

        if(currentGeoPoint==null){
            drawMyPoint = getLastKnownPoint();
        }

       else {

           drawMyPoint = currentGeoPoint;
       }

        OverlayItem overlayitem = new OverlayItem(drawMyPoint, "Moja adresa:", getAddress(currentLocation));
        currPos.addOverlay(overlayitem);
        overlays.add(currPos);
        currPos.setCurrentLocation(currentLocation);




    }



    public void drawMallsOverlay(List <ClassMall> malls){

        List <Overlay> overlays = mapView.getOverlays();
        overlays.remove(mallsOverlay);
        Drawable marker = getResources().getDrawable(R.drawable.malls);
        mallsOverlay = new ClassCustomItemizedOverlay<ClassCustomOverlayItem>(marker, mapView);

        if(malls.size() > 0){
            for(ClassMall temp: malls ){
                GeoPoint mallPoint = getLatLon(temp.getAddress());
                ClassCustomOverlayItem overlayItem = new ClassCustomOverlayItem(mallPoint,temp.getName(),temp.getAddress(), temp.getUrl());
                mallsOverlay.addOverlay(overlayItem);
                //Toast.makeText(getApplicationContext(), temp, Toast.LENGTH_SHORT).show();
            }
        }


        overlays.add(mallsOverlay);

    }


    public String getProvider() {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
        criteria.setAccuracy(Criteria.NO_REQUIREMENT);
        String Provider = locationManager.getBestProvider(criteria, true);
        return Provider;
    }


    public  String getAddress (Location location){

        Geocoder geoCoder = new Geocoder(getApplicationContext(), Locale.getDefault());  
        String sAddress = "";

        try{
            List <Address> address = geoCoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
            if(address.size() > 0){
                for(int i = 0; i < address.get(0).getMaxAddressLineIndex(); i++){
                    sAddress += address.get(0).getAddressLine(i) + "\n";
                }
            }

        }
        catch (IOException e){
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
        }

        return sAddress;

    } 

    public GeoPoint getLatLon (String address){
        Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
        GeoPoint point = null;
        List <Address> addresses = null;
        try {
                addresses = geoCoder.getFromLocationName(address, 1);
                if(addresses.size() > 0){
                     point = new GeoPoint((int) (addresses.get(0).getLatitude() * 1E6), 
                                                  (int) (addresses.get(0).getLongitude()*1E6));
                    }


        } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
        }
        return point;
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

}

请你告诉我如何异步获取我的当前位置?

4

1 回答 1

1
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

intent = new Intent(LegalSeeFoods.this, MapDemoActivity.class);
intent.putExtra("lat", location .latitude);
intent.putExtra("long", location .longitude);
startActivity(intent);

MapDemoActivity.class

mapView.getController().setCenter(point); <---动画到位置

于 2012-05-15T08:47:56.113 回答