[英]What to I need to fill to get my location? Like the blue dot google has
I am new to using Google Maps API and have implemented the map in the fragment. 我是使用Google Maps API的新手,并且已在片段中实现了地图。 I have added a marker that on the map. 我在地图上添加了一个标记。 Now i want my location to pop up like google maps have, the blue dot. 现在,我希望我的位置像谷歌地图上的蓝点一样弹出。 Can anyone help me finish this? 谁能帮我完成这个任务? Thanks alot. 非常感谢。
My java file: 我的Java文件:
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// 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);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng some = new LatLng(80.153, 15.2620);
mMap.addMarker(new MarkerOptions().position(some).title("Marker in some"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(some, 18));
}
}
Manifest file have this ACCESS_FINE_LOCATION 清单文件具有此ACCESS_FINE_LOCATION
You need to enable My Location Layer . 您需要启用“ 我的位置图层” 。
Check your app get the Permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION 检查您的应用是否获得权限ACCESS_FINE_LOCATION或ACCESS_COARSE_LOCATION
Enable the My Location layer , use mMap.setMyLocationEnabled(true);
启用“ 我的位置”图层 ,使用mMap.setMyLocationEnabled(true);
If you don't do step 1, you would get SecurityException. 如果不执行步骤1,则将收到SecurityException。 You should override ActivityCompat.OnRequestPermissionsResultCallback
So the final code should look like this: 您应该重写ActivityCompat.OnRequestPermissionsResultCallback
因此最终代码应如下所示:
// Check for permission to access Location
private boolean checkLocationPermission() {
Log.d(TAG, "checkPermission()");
// Ask for permission if it wasn't granted yet
return (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED);
}
// Asks for permissions
private void askPermission() {
Log.d(TAG, "askPermission()");
ActivityCompat.requestPermissions(
this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION
);
}
// Reaction to whether user granted or denied permissions
@Override
public void onRequestPermissionsResult(
int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
Log.d(TAG, "onRequestPermissionsResult()");
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted
if (checkLocationPermission())
mMap.setMyLocationEnabled(true);
} else {
// Permission denied
// TODO
}
break;
}
}
}
and do this in onMapReady
: 并在onMapReady
执行此onMapReady
:
if (checkLocationPermission())
mMap.setMyLocationEnabled(true);
else
askPermission();
Here is the reference: 这里是参考:
https://developers.google.com/maps/documentation/android-api/location#my-location https://developers.google.com/maps/documentation/android-api/location#my-location
https://developers.google.com/maps/documentation/android-api/location#runtime-permission https://developers.google.com/maps/documentation/android-api/location#runtime-permission
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.