【问题标题】:Google maps Android API and current location谷歌地图 Android API 和当前位置
【发布时间】:2016-05-25 03:39:08
【问题描述】:

我正在尝试实现谷歌地图 API。我想在地图上看到我自己的位置,并且我希望它在我显着移动时发生变化。问题是没有错误,但它没有在地图上显示我的位置标记。这是我在 .java 文件中的 onCreate() 方法

private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
public static final String TAG = MapPane.class.getSimpleName();
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map_pane);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    // Create the LocationRequest object
    mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(10 * 1000)        // 10 seconds, in milliseconds
            .setFastestInterval(1 * 1000); // 1 second, in milliseconds

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}//onCreate

这是我认为我在这里缺少一些东西的方法

 public void onConnected(@Nullable Bundle bundle) {
    Log.i(TAG, "Location services connected.");

    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (location == null) {
        Log.i(TAG, "loc exists");
        if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                || checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        }//if
    }//if
    else {
        handleNewLocation(location);
    }//else
}//onConnected

因为我从来没有进入过第二个 if 语句。我想我首先需要用户的许可?在我的设备上手动打开 gps 也不起作用。以及下面我用于在地图上显示标记的方法

private void handleNewLocation(Location location) {
    Log.d(TAG, location.toString());
    double currentLatitude = location.getLatitude();
    double currentLongitude = location.getLongitude();
    LatLng latLng = new LatLng(currentLatitude, currentLongitude);

    MarkerOptions options = new MarkerOptions()
            .position(latLng)
            .title("I am here!");
    mMap.addMarker(options);
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

}//handleNewLocation

我使用了更多的方法,但我认为那里没有缺陷。在我包含的 AndroidManifest.xml 中

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

【问题讨论】:

  • 位置的价值是什么?
  • 它为空,因为我可以看到 "Log.i(TAG, "loc exists");"在我的 Android 监视器中
  • 你是否像本教程developer.android.com/training/location/retrieve-current.html一样添加了onStart()和onStop()方法?
  • 您的控制台是否出现任何错误?
  • 这是我得到的唯一错误:“加载模块描述符类失败:找不到类”com.google.android.gms.dynamite.descriptors.com.google.android.gms. googlecertificates.ModuleDescriptor" "

标签: android google-maps android-studio


【解决方案1】:

您可以使用 LocationManager 代替 google api 客户端

             LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    final Geocoder gc = new Geocoder(this, Locale.getDefault());

    LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            //here you can get current position location.getLatitude(),location.getLongitude();

        }

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

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };

    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

【讨论】:

    【解决方案2】:

    您的代码似乎没有为handlelocation() 方法提供任何位置。

    private GoogleMap mMap;
    private GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;
    public static final String TAG = MapPane.class.getSimpleName();
    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
    LocationListener li;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_pane);
        // Obtain the SupportMapFragment and get notified when the map is reato be used.
        SupportMapFragment mapFragment = (SupportMapFragment)         getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    
     li=new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                handleNewLocation(location);
            }
    
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
    
            }
    
            @Override
            public void onProviderEnabled(String provider) {
    
            }
    
            @Override
            public void onProviderDisabled(String provider) {
    
            }
        };
    
    
      getHandledCurrentLocation();
    }//onCr
    
    private void getHandledCurrentLocation() {
        String locationProvider = LocationManager.NETWORK_PROVIDER;
    
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        Log.e("LOCATION_DEBUG","LOCATION REQUESTED--- ");
        LCM.requestLocationUpdates(locationProvider, 10 * 1000, 0,li);
    
    }
    private void handleNewLocation(Location location) {
     Log.d(TAG, location.toString());
     double currentLatitude = location.getLatitude();
     double currentLongitude = location.getLongitude();
     LatLng latLng = new LatLng(currentLatitude, currentLongitude);
    
     MarkerOptions options = new MarkerOptions()
            .position(latLng)
            .title("I am here!");
     mMap.addMarker(options);
     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    
    }//handleNewLocation
    

    这段代码应该可以工作。

    【讨论】:

      【解决方案3】:

      我发现您的代码存在一些问题。我不认为您正在处理 FusedLocationApi 提供的位置更新。我相信您正在实施LocationListener,即使您的问题中没有提供这部分代码。在这种情况下,您将在您的活动中实现方法“onLocationChanged(Location location)”。这是您应该尝试在地图上设置新位置的方法。 FusedLocationApi 不会自动调用您的handleNewLocation 方法

      您可能需要为您的活动提供完整的代码,然后我们可以为您提供更好的帮助。

      【讨论】:

        猜你喜欢
        • 2017-03-06
        • 1970-01-01
        • 2021-09-11
        • 1970-01-01
        • 1970-01-01
        • 2014-08-07
        • 2020-06-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多