【问题标题】:Setmylocationenabled(true) does not show my location android studioSetmylocationenabled(true) 不显示我的位置 android studio
【发布时间】:2016-02-25 19:27:18
【问题描述】:

所以标题几乎解释了我遇到的错误是什么,有人可以帮助我吗?这就是我所拥有的,我是 android 的新手,还是我打开 mapfragment 后立即获取我的位置的另一种方式?谢谢

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;


public class RutaTinaja extends AppCompatActivity implements OnMapReadyCallback {


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

        MapFragment mapFragment = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    }
    public void onMapReady(GoogleMap map) {


        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;
        }

        map.setMyLocationEnabled(true);

【问题讨论】:

    标签: android google-maps android-studio


    【解决方案1】:

    启用且位置可用时,my-location 层会持续绘制用户当前位置和方位的指示,displays UI controls 允许用户与其位置进行交互(例如,启用或禁用摄像头跟踪它们的位置和方位)。 要使用 my-location-layer 功能,您需要请求 ACCESS_COARSE_LOCATION 或 ACCESS_FINE_LOCATION 的权限,除非您设置了custom location source

    如果您通过 setLocationSource(LocationSource) 设置了自定义位置源,Google Maps Android API 将不会检查是否已授予上述权限。但是,您仍然需要确保用户已授予您自定义位置源所需的所有权限。

    首先,确保您的 API 密钥有效并将其添加到您的清单中:

    这是一个简单的code,它会询问用户的位置,然后一旦知道了用户的位置,地图就会以他们的位置为中心:

    请参考以下链接: https://stackoverflow.com/a/25789758 https://stackoverflow.com/a/19109093

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2019-03-30
      • 2018-05-10
      • 1970-01-01
      • 1970-01-01
      • 2015-07-04
      • 2013-04-15
      • 2016-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多