如何在Google Maps for Android API v2上显示我的位置

Yos*_*233 57 android geolocation google-maps-android-api-2

对于这方面的答案,我看起来很高很低,没有人,在任何论坛问题上都能提供帮助.我搜索过这些教程.该API指南说:

仅当启用"我的位置"图层时,"我的位置"按钮才会显示在屏幕的右上角.

所以我一直在寻找这个我的位置图层,但一直找不到任何东西.如何在Google地图上显示我的位置?

Yos*_*233 145

API指南完全没错(真的是谷歌吗?).使用Maps v2,您无需启用图层即可自行显示,只需调用您使用地图创建的GoogleMaps实例即可.

Google文档

Google提供的实际文档可为您提供答案.呼叫

// map is a GoogleMap object
map.isMyLocationEnabled = true
Run Code Online (Sandbox Code Playgroud)

并观看魔术的发生.

  • 是否有任何方法可以自动点击MyLocationButton来使地图居中?现在我必须使用LocationClient来做与MyLocationButton相同的操作. (10认同)

Udi*_*diT 39

Java代码:

public class MapActivity extends FragmentActivity implements LocationListener  {

    GoogleMap googleMap;
    LatLng myPosition;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        // Getting reference to the SupportMapFragment of activity_main.xml
        SupportMapFragment fm = (SupportMapFragment)
        getSupportFragmentManager().findFragmentById(R.id.map);

        // Getting GoogleMap object from the fragment
        googleMap = fm.getMap();

        // Enabling MyLocation Layer of Google Map
        googleMap.setMyLocationEnabled(true);

        // Getting LocationManager object from System Service LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        // Creating a criteria object to retrieve provider
        Criteria criteria = new Criteria();

        // Getting the name of the best provider
        String provider = locationManager.getBestProvider(criteria, true);

        // Getting Current Location
        Location location = locationManager.getLastKnownLocation(provider);

        if (location != null) {
            // Getting latitude of the current location
            double latitude = location.getLatitude();

            // Getting longitude of the current location
            double longitude = location.getLongitude();

            // Creating a LatLng object for the current location
            LatLng latLng = new LatLng(latitude, longitude);

            myPosition = new LatLng(latitude, longitude);

            googleMap.addMarker(new MarkerOptions().position(myPosition).title("Start"));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

activity_map.xml:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:map="http://schemas.android.com/apk/res-auto"
  android:id="@+id/map"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  class="com.google.android.gms.maps.SupportMapFragment"/>
Run Code Online (Sandbox Code Playgroud)

您将获得当前位置的蓝色圆圈.


Vas*_*hev 17

从android 6.0你需要检查用户权限,如果你想使用GoogleMap.setMyLocationEnabled(true)你将得到Call requires permission which may be rejected by user错误

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
   mMap.setMyLocationEnabled(true);
} else {
// Show rationale and request permission.
}
Run Code Online (Sandbox Code Playgroud)

如果您想了解更多信息,请查看Google地图文档


Fab*_*Lor 15

要显示"我的位置"按钮,您必须致电

map.getUiSettings().setMyLocationButtonEnabled(true);
Run Code Online (Sandbox Code Playgroud)

在您的GoogleMap对象上.

  • 但首先地图需要显示用户位置.这就是我所要求的.如果没有蓝点,按钮就不好了. (2认同)
  • 如果你正在使用MapView:map.setMyLocationEnabled(true); map.getUiSettings()setMyLocationButtonEnabled(真).两者都需要写 (2认同)

Ter*_*ogy 7

打电话给GoogleMap.setMyLocationEnabled(true)Activity,并在下面添加这两行代码Manifest:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Run Code Online (Sandbox Code Playgroud)