I've looked high and low for an answer on this, and no one, in any forum question has been able to help. I've searched through the tutorials. The API Guide says:
我到处寻找这个问题的答案,在任何论坛问题上都没有人能帮上忙。我搜索了教程。API指南说:
The My Location button appears in the top right corner of the screen only when the My Location layer is enabled.
My Location按钮只在启用My Location层时出现在屏幕的右上角。
So I've been looking for this My Location layer and have been unable to find anything. How do I show my location on a Google Map?
所以我一直在寻找我的位置层,却找不到任何东西。如何在谷歌地图上显示我的位置?
5 个解决方案
#1
121
The API Guide has it all wrong (really Google?). With Maps v2 you do not need to enable a layer to show yourself, there is a simple call to the GoogleMaps instance you created with your map.
API指南完全错了(真的是谷歌吗?)使用Maps v2,您不需要启用一个层来显示自己,只需调用您使用map创建的GoogleMaps实例。
谷歌文档
The actual documentation that Google provides gives you your answer. Call
谷歌提供的实际文档给出了您的答案。调用
// map is a GoogleMap object
map.setMyLocationEnabled(true);
and watch the magic happen.
看着奇迹发生。
#2
31
Java code:
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"));
}
}
}
activity_map.xml:
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"/>
You will get your current location in a blue circle.
你将得到你当前的位置在一个蓝色的圆圈。
#3
12
From android 6.0 you need to check for user permission, if you want to use GoogleMap.setMyLocationEnabled(true)
you will get Call requires permission which may be rejected by user
error
如果你想要使用GoogleMap.setMyLocationEnabled(true),你需要得到允许,这可能会被用户错误拒绝。
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
// Show rationale and request permission.
}
if you want to read more, check google map docs
如果你想阅读更多,请查看谷歌地图文档
#4
11
To show the "My Location" button you have to call
要显示“我的位置”按钮,您必须调用
map.getUiSettings().setMyLocationButtonEnabled(true);
on your GoogleMap object.
在你GoogleMap对象。
#5
7
Call GoogleMap.setMyLocationEnabled(true)
in your Activity
, and add this 2 lines code in the Manifest
:
在您的活动中调用GoogleMap.setMyLocationEnabled(true),并在清单中添加这两行代码:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
#1
121
The API Guide has it all wrong (really Google?). With Maps v2 you do not need to enable a layer to show yourself, there is a simple call to the GoogleMaps instance you created with your map.
API指南完全错了(真的是谷歌吗?)使用Maps v2,您不需要启用一个层来显示自己,只需调用您使用map创建的GoogleMaps实例。
谷歌文档
The actual documentation that Google provides gives you your answer. Call
谷歌提供的实际文档给出了您的答案。调用
// map is a GoogleMap object
map.setMyLocationEnabled(true);
and watch the magic happen.
看着奇迹发生。
#2
31
Java code:
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"));
}
}
}
activity_map.xml:
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"/>
You will get your current location in a blue circle.
你将得到你当前的位置在一个蓝色的圆圈。
#3
12
From android 6.0 you need to check for user permission, if you want to use GoogleMap.setMyLocationEnabled(true)
you will get Call requires permission which may be rejected by user
error
如果你想要使用GoogleMap.setMyLocationEnabled(true),你需要得到允许,这可能会被用户错误拒绝。
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
// Show rationale and request permission.
}
if you want to read more, check google map docs
如果你想阅读更多,请查看谷歌地图文档
#4
11
To show the "My Location" button you have to call
要显示“我的位置”按钮,您必须调用
map.getUiSettings().setMyLocationButtonEnabled(true);
on your GoogleMap object.
在你GoogleMap对象。
#5
7
Call GoogleMap.setMyLocationEnabled(true)
in your Activity
, and add this 2 lines code in the Manifest
:
在您的活动中调用GoogleMap.setMyLocationEnabled(true),并在清单中添加这两行代码:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />