相关疑难解决方法(0)

在Android上获取用户当前位置的最简单,最强大的方法是什么?

LocationManagerAndroid上的API似乎对于仅需要偶尔粗略估计用户位置的应用程序来说有点痛苦.

我正在处理的应用程序本身并不是一个位置应用程序,但它确实需要获取用户的位置才能显示附近的商家列表.它不需要担心用户是否在移动或类似的东西.

这是我想做的事情:

  1. 向用户显示附近位置的列表.
  2. 预加载用户的位置,以便在ActivityX中需要它时,它将可用.
  3. 我并不特别关心更新的准确性或频率.只要没有办法,抓住一个位置就足够了.也许如果我想要花哨,我会每隔几分钟更新一次位置,但这不是一个重要的优先事项.
  4. 只要有GPS或网络位置提供商,就可以使用任何设备.

看起来它应该不那么难,但在我看来,我必须启动两个不同的位置提供商(GPS和NETWORK)并管理每个生命周期.不仅如此,我还必须在多个活动中复制相同的代码以满足#2.我曾尝试过使用getBestProvider()过去将解决方案缩减为只使用一个位置提供商,但这似乎只能为您提供最好的"理论"提供商,而不是实际上会给您带来最佳结果的提供商.

有没有更简单的方法来实现这一目标?

android location geolocation latitude-longitude

788
推荐指数
12
解决办法
31万
查看次数

Android - 如何获取当前位置(经度和纬度)?

我使用以下代码来获取当前位置即(纬度和经度),但我没有获得当前位置(纬度和经度).

谁知道为什么?

package com.ram.currentlocation;

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

public class Location_Gps extends Activity
{

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

      /* Use the LocationManager class to obtain GPS locations */
      LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

      LocationListener mlocListener = new MyLocationListener();
      mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
    }

    /* Class My Location Listener */
    public class MyLocationListener implements LocationListener
    {

      @Override
      public …
Run Code Online (Sandbox Code Playgroud)

android geolocation latitude-longitude google-latitude geocode

17
推荐指数
2
解决办法
7万
查看次数

我当前的位置始终返回null.我怎样才能解决这个问题?

我想找到一个Android项目的当前位置.加载应用程序时,我的当前位置始终为null.我已经在清单等中设置了权限.当我找到当前位置时,我打算使用坐标来查找到地图上其他位置的距离.我的代码片段如下.为什么我总是得到一个空值?

locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);          
Criteria crit = new Criteria();     
towers = locMan.getBestProvider(crit, false);
location = locMan.getLastKnownLocation(towers);    

if (location != null) {                 
    System.out.println("Location is not null!");
    lat = (int) (location.getLatitude() *1E6);
    longi = (int) (location.getLongitude() * 1E6);
    GeoPoint ourLocation = new GeoPoint(lati, longi);
    OverlayItem overlayItem = new OverlayItem(ourLocation, "1st String",
                                                            "2nd String");
    CustomPinpoint custom = new CustomPinpoint(d, MainMap.this);
    custom.insertPinpoint(overlayItem);
    overlayList.add(custom);
    overlayList.clear();
} else {
   System.out.println("Location is null! " + towers);
   Toast.makeText(MainMap.this, "Couldn't get provider",Toast.LENGTH_SHORT)
                                                                    .show();
}
Run Code Online (Sandbox Code Playgroud)

null android location google-maps nullpointerexception

17
推荐指数
2
解决办法
4万
查看次数

如何在Android应用程序中获取当前的经纬度?

我想找到我在android应用程序中当前位置的纬度和经度.它不是在位置上更改.我获得了纬度和经度值而位置Changed.but我需要当前位置的纬度和经度.如何做到这一点?

android latitude-longitude

5
推荐指数
1
解决办法
1万
查看次数

如何在Android中获得经度和纬度

我想找到我当前位置的经度和纬度,但我一直得到NULL.

double lat = loc.getLatitude(); //Cause the result is null, so can't know longitude and latitude
double lng = loc.getLongitude();


 LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);  
    String provider = LocationManager.GPS_PROVIDER;  
    Location location = locationManager.getLastKnownLocation(provider); //result is null!
Run Code Online (Sandbox Code Playgroud)

这是获取GPS状态的代码.它工作正常:

public void onGpsStatusChanged(int event) { // get the GPS statue  
                LocationManager locationManager = (LocationManager) GpsActivity.this.getSystemService(Context.LOCATION_SERVICE);  
                GpsStatus status = locationManager.getGpsStatus(null);  
                String satelliteInfo = updateGpsStatus(event, status);
                myTextView.setText(satelliteInfo);//work fine ,searched satellite:16
    }
    };
        private String updateGpsStatus(int event, GpsStatus status) {  
            StringBuilder sb2 = new …
Run Code Online (Sandbox Code Playgroud)

android android-location

3
推荐指数
1
解决办法
5316
查看次数

即使使用最佳提供程序,getLastKnownLocation()也会返回null

这是我用来获取当前位置的代码:

mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
best = mgr.getBestProvider(criteria, true);
if (best == null) {
    //ask user to enable atleast one of the Location Providers
} else {
    Location location = mgr.getLastKnownLocation(best);
//But sometimes it returns null
}
Run Code Online (Sandbox Code Playgroud)

几乎每次都有 best = network

但它有时并没有提供这个位置.

mgr.getLastKnownLocation(best) returns null
Run Code Online (Sandbox Code Playgroud)

也:

onResume() {
    mgr.requestLocationUpdates(best, 15000, 10, this);
}
Run Code Online (Sandbox Code Playgroud)

onPause() {
    mgr.removeUpdates(this);
}
Run Code Online (Sandbox Code Playgroud)

这种情况有替代方案吗?

一种选择可能是

List<String> providers = mgr.getAllProviders();
Run Code Online (Sandbox Code Playgroud)

存储所有提供商并逐步减少1.但从未在任何地方看到过此推荐.此外,要求最好的提供者是文档建议的.

android location locationmanager

1
推荐指数
1
解决办法
8060
查看次数