在 Android 中显示用户的位置

发布于 2024-10-11 15:37:32 字数 1461 浏览 1 评论 0原文

我在获取用户位置(我的位置)时遇到问题。我的代码是

double lat;
double lng;
LocationManager locationManager;
String context = Context.LOCATION_SERVICE; 
locationManager = (LocationManager)getSystemService(context);
String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
if(!locationManager.isProviderEnabled(provider)){
 locationManager.setTestProviderEnabled(provider, true);
}
boolean enabled = locationManager.isProviderEnabled(provider);
if(enabled){
        Toast.makeText(LoginActivity.this,"provider enabled",Toast.LENGTH_LONG).show();
}
else{
    Toast.makeText(LoginActivity.this,"provider disabled",Toast.LENGTH_LONG).show();
}
if(location!=null){
  lat = location.getLatitude();
  lng = location.getLongitude();
  AlertDialog.Builder ab=new AlertDialog.Builder(LoginActivity.this);
  ab.setMessage(Html.fromHtml("<b><font color=#ff0000>Location" +"</font></b><br>"
            +location.toString()+"<br>Latitude: "+lat
                +"<br>Longitude "+lng));
  ab.setPositiveButton("ok",null );
  Toast.makeText(LoginActivity.this,"You are at     "+location.toString(),Toast.LENGTH_LONG).show();
}
else{
    Toast.makeText(LoginActivity.this,"Location not found",Toast.LENGTH_LONG).show();
} 

我的问题是,在启用应用程序消息提供程序时,我将位置获取为 null。我没有发现这段代码有任何问题。我还在设备上对其进行了测试,它显示提供程序已启用,但未找到位置。

我还没有在类中实现位置侦听器。 是否有必要在我的班级中实现位置侦听器?

I have a problem in getting the user's location (my location). My code is

double lat;
double lng;
LocationManager locationManager;
String context = Context.LOCATION_SERVICE; 
locationManager = (LocationManager)getSystemService(context);
String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
if(!locationManager.isProviderEnabled(provider)){
 locationManager.setTestProviderEnabled(provider, true);
}
boolean enabled = locationManager.isProviderEnabled(provider);
if(enabled){
        Toast.makeText(LoginActivity.this,"provider enabled",Toast.LENGTH_LONG).show();
}
else{
    Toast.makeText(LoginActivity.this,"provider disabled",Toast.LENGTH_LONG).show();
}
if(location!=null){
  lat = location.getLatitude();
  lng = location.getLongitude();
  AlertDialog.Builder ab=new AlertDialog.Builder(LoginActivity.this);
  ab.setMessage(Html.fromHtml("<b><font color=#ff0000>Location" +"</font></b><br>"
            +location.toString()+"<br>Latitude: "+lat
                +"<br>Longitude "+lng));
  ab.setPositiveButton("ok",null );
  Toast.makeText(LoginActivity.this,"You are at     "+location.toString(),Toast.LENGTH_LONG).show();
}
else{
    Toast.makeText(LoginActivity.this,"Location not found",Toast.LENGTH_LONG).show();
} 

My problem is I am getting the location as null while the application message provider is enabled. I have not found any problem in this code. I have also tested it on a device, it shows the provider is enabled and the location isn't found.

I have not implemented the location listener in the class.
Is it necessary to implement location listener in my class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半仙 2024-10-18 15:37:32

您只能通过手机获取最后已知的位置。如果该值为空(如果没有最后一个已知位置可用,则为空),则您不会尝试以任何其他方式接收位置。

您应该实现一个 LocationListener 并根据本指南注册它以接收位置更新: http://developer.android.com/guide/topics/location/obtaining-user-location.html 这将导致手机尝试获取用户位置并将其移交给您的应用程序Location 对象的形式。

You are only getting the last known location from the phone. If this is null, which it is if no last known location is available, you are not trying to receive locations in any other way.

You should implement a LocationListener an register it to receive location updates according to this guide: http://developer.android.com/guide/topics/location/obtaining-user-location.html This will cause the phone to try to get the users location and hand it over to your application in the form of a Location object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文