2012-05-15 3 views
2

하나의 활동에서 나의 활동을 얻고 싶습니다. 다른 활동으로 보내고 싶습니다. (버튼을 클릭하면)지도에 내 위치가 표시됩니다. 여기에 (내가 처음 활동이 현재 위치를 얻을하지 않습니다 생각, 문제는,지도 내 현재 위치에 애니메이션을하지 않고 두 번째 활동에 널 전송) 내 코드입니다 :하나의 활동에서 다른 활동으로 현재 위치 보내기

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; 
    } 

}에 대한 코드는

그리고 당신은 내가 비동기 내 현재 위치를 얻을 수있는 방법을 말해 줄 수주십시오? MapDemoActivity.class

mapView.getController().setCenter(point);< --- IN

+0

는 개봉 위치를받지 확실 위치로 애니메이션? 아마 그것을 검색 할 수 있지만 어떤 이유로 그것을 다음 활동으로 보낼 수 없습니다. 위치 관리자가 좌표를 수신했는지 확인하기 위해 위치를 얻은 직후에 로그 명령문을 추가하십시오. – Urban

+0

문제를 해결 했습니까? –

+0

더 이상 기억할 수 없습니다, 미안 해요 :/(해결 했습니까, 내가 가지고있는 경우) – hyperN

답변

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); 

+0

하시겠습니까? – MAC

+0

당신의 솔루션을 시도했지만 널 포인터 예외가 발생했습니다 : ava.lang.RuntimeException : 활동을 시작할 수 없습니다 ComponentInfo {com.projekt.app/com.projekt.app.ActivityMap} : java.lang.NullPointerException ... ActivityMap – hyperN

+0

@MAC 내 관련 질문도 참조하십시오 : http://stackoverflow.com/questions/24851013/send-data-from-one-activity-to-another-regularly –

관련 문제