2011-06-02 92 views
0

所有我想要做的是返回从最后一个知道位置的长/纬度值,如果这是null然后拉新的coords。Android位置管理器?

我很新的这一点,这是我有:

import android.app.Activity; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.widget.TextView; 

public class GPS extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
     Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     double longitude = location.getLongitude(); 
     double latitude = location.getLatitude(); 


     TextView output; 

     output = (TextView) findViewById(R.id.output); 


     output.append("\n\nLocations (starting with last known):"); 
     Location location = locationManager.getLastKnownLocation(bestProvider); 
     printLocation(location); 



    } 
} 
+0

你得到了什么错误或者你的EditText输出是什么? – 2011-06-02 16:40:19

+0

它只是强制关闭,我甚至不能运行它。 – Nick 2011-06-02 16:44:35

+0

在这种情况下,您应该发布LogCat输出。 – 2011-06-02 16:53:38

回答

1

而且不要忘了ACCESS_COARSE_LOCATIONACCESS_FINE_LOCATION权限添加到您的AndoidManifest.xml。

+0

我有Fine_location但我添加了coarse_location - 仍然强制关闭。谢谢你的建议,我确信这是许多错误之一。 – Nick 2011-06-02 16:52:54

+0

如果您要求'ACCESS_FINE_LOCATION'(并授予)您不需要'ACCESS_COARSE_LOCATION',则会自动授予。 – slinden77 2012-08-22 01:51:42

1

检查位置,如果他们都为空,那么你必须使用 locationManager.requestLocationUpdates(...)函数。

我想你也必须实现一个位置监听器,以在现场条件下有效地处理这个事情。

我已经回答here