2013-10-30 2 views
0

이상한 변수 설정 문제가있는 것 같습니다. 제대로 업데이트하고 현재 위치를 올바르게 설정하는 LocationListener가 있습니다. 나는 현재의 위도/경도를 포함하는 전역 변수를 설정하려고 시도하는데, 어떤 종류의 코드를 게시 한 후에는 그 이상이 실패합니다.LocationListener 내에서 이중 설정하기

나는 조각이

public class MyMap extends MapFragment { 
    private LocationManager mLocManager; 
    private LocationListener myLocationListener; 
    protected volatile double lat;//Place to hold variable 
    protected volatile double lon;//Place to hold variable 

    public void onActivityCreated(Bundled savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this.getActivity().getApplicationContext()); 
    if (resultCode == ConnectionResult.SUCCESS){ 

     if(gmap == null){ 
      gmap = this.getMap(); 
      if(gmap != null){ 
       gmap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
       gmap.setOnMarkerDragListener(this); 
       gmap.setMyLocationEnabled(true); 
       gmap.setOnMapLongClickListener(this); 
      }else{ 
       return; 
      } 
     } 
    }else{ 
     GooglePlayServicesUtil.getErrorDialog(resultCode, this.getActivity(), RQS_GooglePlayServices); 
    } 
    distance = 100; 
    myLocationListener = new MyLocationListener(); 
    mLocManager = (LocationManager) this.getActivity().getSystemService(Context.LOCATION_SERVICE); 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500,10, myLocationListener); 
    mLocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 10, myLocationListener); 
    mLocManager.getLastKnownLocation(Context.LOCATION_SERVICE); 

} 

@Override 
public void onPause() { 
    super.onPause(); 
    mLocManager.removeUpdates(myLocationListener); 
} 

//Listen for location changes and update the map with that information 
private class MyLocationListener implements LocationListener { 

    @Override 
    public void onLocationChanged(Location location) { 
     if (Math.abs(location.getLatitude()) > 0.0 && Math.abs(location.getLongitude()) > 0.0) { 
      lat = location.getLatitude(); 
      lon = location.getLongitude(); 
     } 
     //This reports correctly 
     Log.v("Magic", "LOCATION CHANGED!" + "\nLat: " + Double.toString(lat) + "\nLon: " + Double.toString(lon)); 
     if (!currentLocationSet) { 
      gmap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon), 15)); 
      currentLocationSet = true; 
     } 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 

    } 
} 
public void newPlace(final UpdatePlacesCallBack upc, Place p) { 
    //This gives me 0.0 for both variables. 
    Log.v("Magic", "Name: " + p.getName() + "\nLat: " + Double.toString(lat) + "\nLon:" + Double.toString(lon)); 
}  

} 

그럼, 어떻게하면 내가 LocationListener 내에서 로그를 볼 때 정확한 위치를 기록 할 것이다,하지만 나는 이중 위도의 값을 검색하려고 경우 double lon 두 변수 모두 0.0을 반환합니다.

+0

어디에서 newPlace() 메서드를 호출합니까? MyMap 클래스 내부 또는 외부? – user2652394

+0

MapFragment를 생성하는 Activity 외부에서 그것을 호출합니다. OnClickListener가있는 서랍에는 푸시 할 때 정보를 묻는 대화 상자가 표시되고 Place 객체가 만들어지고 MapFragment로 전달됩니다. – gochuck

답변

1
// try this 
public static LocationManager mlocManager; 
public static LocationListner mListner; 
private static String latitude; 
public static String longitude; 
private Location currentLocation; 

public Location getCurrentLocation() { 
    return currentLocation; 
} 

public void setCurrentLocation(Location currentLocation) { 
    this.currentLocation = currentLocation; 
} 

try { 
mlocManager = (LocationManager) getSystemService(Activity.LOCATION_SERVICE); 
       mListner = new LocationListner(); 
       runOnUiThread(new Runnable() { 

        @Override 
        public void run() { 
         try { 
         try { 
          mlocManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, mListner); 
         } catch (Throwable e) { 
          e.printStackTrace(); 
         } 

          mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mListner); 
         } catch (Throwable e) { 
          e.printStackTrace(); 
         } 
         try { 
          mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mListner); 
         } catch (Throwable e) { 
          e.printStackTrace(); 
         } 

        } 
       }); 
} catch (Throwable e) { 
    e.printStackTrace(); 
} 

    public String getLatitude() { 
     if (latitude != null) { 
      return latitude; 
     } 
     Location loc = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     if (loc == null) { 
      loc = mlocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
      if (loc == null) { 
       loc = mlocManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); 
      } 
      if (loc != null) { 
       return "" + loc.getLatitude(); 
      } 
     } else { 
      return "" + loc.getLatitude(); 
     } 
     return "0.0"; 
    } 

    public void setLatitude(String latitide) { 
     latitude = latitide; 
    } 

    public String getLongitude() { 
     if (longitude != null) { 
      return longitude; 
     } 
     Location loc = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     if (loc == null) { 
      loc = mlocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
      if (loc == null) { 
       loc = mlocManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); 
      } 
      if (loc != null) { 
       return "" + loc.getLongitude(); 
      } 
     } else { 
      return "" + loc.getLongitude(); 
     } 
     return "0.0"; 
    } 

    public void setLongitude(String longitude) { 
     SmartActivity.longitude = longitude; 
    } 

    class LocationListner implements LocationListener { 

     @Override 
     public void onLocationChanged(Location location) { 
      setLatitude("" + location.getLatitude()); 
      setLongitude("" + location.getLongitude()); 
      setCurrentLocation(location); 
     } 

     @Override 
     public void onProviderDisabled(String provider) { 
     } 

     @Override 
     public void onProviderEnabled(String provider) { 
     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
     } 

    } 
+0

안녕하세요, 감사합니다. 감사합니다. 감사합니다. –

관련 문제