【问题标题】:Android Google Maps API LocationAndroid 谷歌地图 API 位置
【发布时间】:2014-08-07 14:47:34
【问题描述】:

我用谷歌搜索了一下,仍然找不到明确的答案。

目前在 Android 中使用 Google 地图的“最佳”或“推荐”方式是什么?

我希望我的应用程序使用自动跟踪用户位置的蓝点,就像您单击“我的位置”按钮时地图应用程序所做的那样。其中一些功能似乎是通过使用地图内置的,但是,我不确定如何从中提取位置,因为我已经阅读过 .getMyLocation() 已弃用?

最好的方法是什么?

【问题讨论】:

    标签: android google-maps google-maps-android-api-2


    【解决方案1】:

    我正在使用此代码:

    //get locationManager object from System Service LOCATION_SERVICE
                LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    
    
                Criteria criteria = new Criteria();
    
                String provider = locationManager.getBestProvider(criteria, true);
    
                Location myLocation = locationManager.getLastKnownLocation(provider);
    
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    
                double latitude = myLocation.getLatitude();
                double longitude = myLocation.getLongitude();
    
                LatLng latLng = new LatLng(latitude, longitude);
    
    
                map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));
    

    【讨论】:

    • 会不断更新,还是需要回调以确保数据是最新和最准确的可用数据?
    • 说实话,我在使用的时候并没有走太多路,我每次启动应用程序时都会运行这个函数,所以我会说也许你需要一个回调。
    【解决方案2】:

    您可以通过启用“我的位置”图层以蓝点显示用户的当前位置。只需调用以下方法,显示用户当前位置的蓝点就会显示在地图上:

    mMap.setMyLocationEnabled(true);
    

    要获取位置更新,您应该使用LocationClient 设置LocationListener。当您实现LocationListener 接口时,您将覆盖onLocationChanged 方法,该方法将为您提供位置更新。当您使用LocationClient 请求位置更新时,您会创建一个LocationRequest 来指定您希望位置更新的频率。

    There is an excellent tutorial at the Android Developers site for making you app location aware.

    【讨论】:

      【解决方案3】:

      您必须使用位置侦听器。它会解决你想要的一切。 使用此代码。我试图解释 cmets 中的所有内容。

      import android.location.Location;
      import android.os.Bundle;
      import android.support.v4.app.FragmentActivity;
      
      import com.google.android.gms.common.ConnectionResult;
      import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
      import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
      import com.google.android.gms.location.LocationClient;
      import com.google.android.gms.location.LocationListener;
      import com.google.android.gms.location.LocationRequest;
      import com.google.android.gms.maps.CameraUpdate;
      import com.google.android.gms.maps.CameraUpdateFactory;
      import com.google.android.gms.maps.GoogleMap;
      import com.google.android.gms.maps.SupportMapFragment;
      import com.google.android.gms.maps.model.CameraPosition;
      import com.google.android.gms.maps.model.LatLng;
      
      public class YourActivity extends FragmentActivity implements 
      LocationListener, ConnectionCallbacks, OnConnectionFailedListener{
      
          private GoogleMap mMap;
          private LocationClient mLocationClient;
          private static final LocationRequest REQUEST = LocationRequest.create()
                  .setInterval(1000)         // 1 second - interval between requests
                  .setFastestInterval(16)    // 60fps
                  .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.your_map);
      
          }
      
          @Override
          protected void onResume() {
              super.onResume();
              setUpMapIfNeeded();
              setUpLocationClientIfNeeded();
              //.. and connect to client
              mLocationClient.connect();
      
          }
      
          @Override
          public void onPause() {
              super.onPause();
              // if you was connected to client you have to disconnect
              if (mLocationClient != null) {
                  mLocationClient.disconnect();
              }
          }
      
          private void setUpMapIfNeeded() {
              // if map did not install yet
              if (mMap == null) {
                  // install it from fragment
                  mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                          .getMap();
                  // if installation success
                  if (mMap != null) {
                      // init all settings of map
                      init();
                  }
              }
          }
      
          //if client was not created - create it
          private void setUpLocationClientIfNeeded() {
              if (mLocationClient == null) {
                  mLocationClient = new LocationClient(
                          getApplicationContext(),
                          this, 
                          this);
              }
          }
      
          private void init() {
              // settings of your map
              mMap.setBuildingsEnabled(true);
              mMap.setMyLocationEnabled(true);
              mMap.getUiSettings().setCompassEnabled(true);
              mMap.getUiSettings().setMyLocationButtonEnabled(true);
      
          }
      
          // when you get your current location - set it on center of screen
          @Override
          public void onLocationChanged(Location location) {
      
              CameraPosition cameraPosition = new CameraPosition.Builder()
              .target(new LatLng(location.getLatitude(), location.getLongitude())).zoom(9).bearing(0).tilt(0).build();
              CameraUpdate cameraUpdate = CameraUpdateFactory
              .newCameraPosition(cameraPosition);
              mMap.animateCamera(cameraUpdate);
      
              //Be careful, if want to show current location only once(after starting map) use this line
              //but you don't need it in your task!
              mLocationClient.removeLocationUpdates(this);
          }
      
          // after connection to client we will get new location from LocationListener 
          //with settings of LocationRequest
          @Override
          public void onConnected(Bundle arg0) {
              mLocationClient.requestLocationUpdates(
                      REQUEST, //settings for this request described in "LocationRequest REQUEST" at the beginning
                      this);
          }
      
          @Override
          public void onDisconnected() {
      
          }
      
          @Override
          public void onConnectionFailed(ConnectionResult arg0) {
      
          }
      }
      

      【讨论】:

        【解决方案4】:

        我过去 2 天一直在寻找它。
        回答你的问题:

        目前在 Android 中使用 Google 地图的“最佳”或“推荐”方式是什么?

        今天你有两个选择:

        1. 使用新的/最新的位置服务 API(如谷歌推荐)。但是有一个“小”问题:官方指南Location APIs 已经过时了。我的意思是,如果您按照他们的教程 Making Your App Location-Aware 进行操作,您会发现他们正在使用已弃用的 LocationClient。在寻找“新”方法 2 天后,我找到了这个不错的答案 Android LocationClient class is deprecated but used in documentation

        2. 使用LocationManager。这第二个是最常用的。你可以找到如何使用它herehere

        据我所知,两者都可以正常工作,但如果你问我将使用哪一个,我会选择 Google 推荐的一个:#1。

        他们是这样说的:

        Google 位置服务 API 是 Google Play 服务的一部分,它提供了一个更强大的高级框架,可以自动处理位置提供者、用户移动和位置准确性。它还根据您提供的功耗参数处理位置更新调度。在大多数情况下,通过使用定位服务 API,您可以获得更好的电池性能和更合适的准确度。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-10
          相关资源
          最近更新 更多