GPS位置仅返回null(0.0,0.0)

时间:2014-07-10 13:42:37

标签: android google-maps gps

我有Android设备的应用程序,有多个地址。通过点击任何地址,想要获取用户的当前位置并在谷歌地图(http://maps.google.com/maps?&saddr =“)上播放链接,以及目的地的坐标。

但是,“纬度”和“经度”的结果仅返回“0.0”。

任何人都可以帮助我吗?

抱歉英文不好。

public class Guaruja extends Activity Activity implements LocationListener {

Button bttela2;
TextView t1;
Uri uri;
    protected LocationManager locationManager;
    protected LocationListener locationListener;
    protected Context context;
String provider;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.guaruja);

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


    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

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


    TextView1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String lati = String.valueOf(latitude);
            String longi = String.valueOf(longitude);
            String firt = "http://maps.google.com/maps?&saddr=";
            String secord="&daddr=-22.748085, -47.369444";
            String virgula=",";
            String url= firt+latitude+virgula+longitude+secord;
            uri=Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);

        }


    });


}


public void onLocationChanged(Location location){

       latitude = location.getLatitude() * 1E6;
       longitude = location.getLongitude() * 1E6;

    }

   public void onProviderDisabled(String provider) {
    Log.d("Latitude","disable");
}


public void onProviderEnabled(String provider) {
    Log.d("Latitude","enable");
}


public void onStatusChanged(String provider, int status, Bundle extras) {
    Log.d("Latitude","status");
}

权限:

    <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

2 个答案:

答案 0 :(得分:1)

在Android Developer的网站上查看这篇文章:

http://developer.android.com/guide/topics/location/strategies.html

如上面的评论中所述,您需要实际从位置服务请求更新。否则,你永远不会得到回调。

locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);

你会注意到最后一个参数是一个LocationListener对象。这是处理回调的对象。我看到你有“onLocationChanged”方法,但你的类没有实现LocationListener接口,所以该方法不会做任何事情。

此外,访问粗略位置的权限是多余的,因为它隐含了精确的位置权限。

答案 1 :(得分:0)

我没有通过编程接收位置,而是谷歌为我工作。如果您将链接的字段“起始点”留空(“&amp; saddr =”),则坐标将为“当前位置”。

Google地图上的链接必须如下:“http://maps.google.com/maps?&saddr=&daddr=-23.990624,-46.282269&sensor=TRUE”。