Java 无法在我的android设备上获取经度和纬度

Java 无法在我的android设备上获取经度和纬度,java,android,geolocation,gps,Java,Android,Geolocation,Gps,我有一个显示GPS经度和纬度的应用程序。如果我使用LocationManager.NETWORK\u提供程序,它可以显示网络的经度和纬度。但当我使用LocationManager.GPS\u提供程序时,它不会显示任何内容。但当我使用谷歌地图时,它可以指出我的位置。谷歌地图如何获取我的位置?我需要得到我的设备的经度和纬度 这是我的密码 import android.location.LocationManager; import android.location.Location; import

我有一个显示GPS经度和纬度的应用程序。如果我使用LocationManager.NETWORK\u提供程序,它可以显示网络的经度和纬度。但当我使用LocationManager.GPS\u提供程序时,它不会显示任何内容。但当我使用谷歌地图时,它可以指出我的位置。谷歌地图如何获取我的位置?我需要得到我的设备的经度和纬度

这是我的密码

import android.location.LocationManager;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity implements OnClickListener{

double glat;
double glng;


LocationListener glocListener;

TextView longitudetv;
TextView latitudetv;

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

    TextView tv = (TextView) findViewById(R.id.mytextview);
    tv.setVisibility(View.GONE);

    longitudetv = (TextView) findViewById(R.id.textView3);
    longitudetv.setVisibility(View.GONE);

    latitudetv = (TextView) findViewById(R.id.textView4);
    latitudetv.setVisibility(View.GONE);



    Button b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

//This is for Lat lng which is determine by your device GPS
    public class MyLocationListenerGPS implements LocationListener  
    {
        @Override
        public void onLocationChanged(Location loc)
        {
            glat = loc.getLatitude();
            glng = loc.getLongitude();

            //Setting the GPS Lat, Lng into the textView

            longitudetv.setText("" + glng);
            latitudetv.setText("" + glat);


        }

        @Override
        public void onProviderDisabled(String provider)
        {

        }

        @Override
        public void onProviderEnabled(String provider)
        {

        }

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

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v.getId()==R.id.button1){
        LocationManager locman = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        TextView tv = (TextView) findViewById(R.id.mytextview);
        tv.setVisibility(View.VISIBLE);


        if(locman.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            tv.setText("Started!");
            latitudetv.setVisibility(View.VISIBLE);
            longitudetv.setVisibility(View.VISIBLE);
            locman   = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            glocListener = new MyLocationListenerGPS();
            locman.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                1000 * 1,  // 1 Sec        
                       0, // 0 meter   
                       glocListener);

        }
        else{
            tv.setText("There is no GPS Connection");
            Toast.makeText( getApplicationContext(), "GPS is Disabled.Please enable GPS", Toast.LENGTH_SHORT ).show();

        }

    }

}



}
我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.willardcuizon.adaptivedgps"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" /> 

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" android:debuggable="false">
    <activity
        android:name="com.willardcuizon.adaptivedgps.Main"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>


首先,如果您还没有添加,您需要在清单文件中使用访问GPS的权限,在这里

使用权限android:name=“android.permission.ACCESS\u FINE\u位置

现在您已经拥有了此权限,接下来我发现缺少的是MyLocationListenerGPS应该扩展服务(因为位置是一项服务)。您可以查看此链接,其中详细说明了如何实现它


首先,如果您还没有添加,您需要在清单文件中使用访问GPS的权限,在这里

使用权限android:name=“android.permission.ACCESS\u FINE\u位置

既然您有了这个权限,接下来我发现缺少的是MyLocationListenerGPS应该扩展服务(因为位置是一项服务)。您可以查看此链接,该链接详细说明了如何实现它


是的,我在我的房子里,但即使我在我的房子里,我也使用 谷歌地图,谷歌地图应用程序仍然可以看到我的位置

问题是谷歌地图使用的是,它可以切换提供商。如果找不到GPS提供商,它将切换到网络提供商。LocationManager无法自动执行此切换。因此,您必须自己处理此切换,或者您也可以使用LocationClient,这是推荐的方法。以下是文档的详细信息,SDK中的示例代码位于
yoursdk/extras/google/google\u play\u services/samples/maps
,这将帮助您如何使用它


是的,我在我的房子里,但即使我在我的房子里,我也使用 谷歌地图,谷歌地图应用程序仍然可以看到我的位置


问题是谷歌地图使用的是,它可以切换提供商。如果找不到GPS提供商,它将切换到网络提供商。LocationManager无法自动执行此切换。因此,您必须自己处理此切换,或者您也可以使用LocationClient,这是推荐的方法。以下是文档的详细信息,SDK中的示例代码位于
yoursdk/extras/google/google\u play\u services/samples/maps
,这将帮助您如何使用它

使用,

使用,

您在清单文件中设置了正确的权限吗?是的,清单中没有问题,我只是不明白为什么谷歌地图可以看到我的位置和我的应用程序不能?但是当我使用网络提供商而不是GPS提供商时,它会告诉我我的网络位置。你在建筑物内吗?显示你的清单文件。是的,我在我的房子内,但即使我在我的房子内,我也使用谷歌地图,谷歌地图应用程序仍然可以看到我的位置。你在清单文件中设置了正确的权限吗?是的,清单中没有问题,我只是不明白为什么谷歌地图可以看到我的位置而我的应用程序不能?但是当我使用网络提供商而不是GPS提供商时,它会显示我的网络位置。你在建筑物内吗?显示你的清单文件。是的,我在我的房子内,但即使我在我的房子内,我使用谷歌地图,谷歌地图应用程序仍然可以看到我的位置。