2013-02-26 44 views
2

我可以通过从片段中使用LocationListener和LocationManager获取用户位置吗?或者,我是否需要在底层活动中使用LocationListener,并使用接口(或其他某种方法)将数据传输回片段?由于更改应用UI,我正在将活动转换为片段。当我使用独立活动时,获取位置并不是问题,但是,现在我无法获取任何返回的位置,因为我已将该活动制作为片段。在片段中使用LocationListener

以下是我如何添加LocationListener。它在之前声明为LocationListener locationListener。

private void addLocationListener(){ 
    locationListener = new LocationListener(){ 

     @Override 
     public void onLocationChanged(Location location) { 
      GeoPoint userLoc = processNewLocation(location); 
      if(userLoc != null){ 
       Log.d("USERLOC", userLoc.toString()); 
            //do something with the location  
      } 
     } 

     @Override 
     public void onProviderDisabled(String provider) { 
      locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE); 
      if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 
      } else { 
       locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
      } 
     } 

     @Override 
     public void onProviderEnabled(String provider) { 
      locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE); 
      if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 
      } else { 
       locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
      } 
     } 

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

     } 

    }; 
} 

注意:processNewLocation只是将位置转换为GeoPoint。它甚至没有那么远,因为没有获得任何位置。

下面是与外景经理

public void addLocationManager(){ 
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); 
} 

private void getLocationFromNetwork(){ 
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, LOCATION_REFRESH_TIME, LOCATION_REFRESH_DISTANCE, locationListener); 
Log.d("GET NETWORK", "Listener registered"); 
} 

private void getLocationFromGPS(){ 
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_REFRESH_TIME, LOCATION_REFRESH_DISTANCE, locationListener); 
Log.d("GET GPS", "Listener registered"); 
} 

private Location getLastLocation(){ 
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
return lastKnownLocation; 
} 
+0

您可以使用片段内的LocationManager&LocationListener。可能需要更多的代码才能找出为什么你没有得到一个位置。 – paul 2013-02-26 21:12:08

+0

首先发布您的locationListener的代码。 – paul 2013-02-26 21:20:16

回答

0

在你上面贴的代码注册监听器的代码,你千万不要注册到的LocationManager的LocationListener的。你需要这样的代码从代码onProviderEnabled在某处片段创建代码:

 locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE); 
     if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 
     } else { 
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
     } 

的obProviderEnabled回调不会发生你外景经理注册后到。

+0

之前有一些代码显示了正在注册的locationListener。显然它在添加更多代码时被删除。 – paul 2013-02-26 23:03:04

+0

我可以再次发布对不起 - 只需要位置监听器代码的评论 – Rarw 2013-02-26 23:36:21