地图不显示带有标记的当前位置
Posted
技术标签:
【中文标题】地图不显示带有标记的当前位置【英文标题】:Map doesnt show current location with marker 【发布时间】:2017-11-20 16:40:22 【问题描述】:我正在使用导航抽屉,在那个选项中我需要为谷歌地图提供一个选项。我想显示用户的当前位置,并用标记在他的位置持续更新。 Foll是我的代码
public class MapView extends Fragment implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener
GoogleMap m_map;
boolean mapReady = false;
private GoogleApiClient googleApiClient;
private LocationListener locationListener;
private Marker marker;
public MapView()
// Required empty public constructor
@Override
public void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_map_view, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return view;
@Override
public void onMapReady(GoogleMap googleMap)
mapReady = true;
m_map = googleMap;
googleApiClient = new GoogleApiClient.Builder(getActivity()).addApi(LocationServices.API).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
googleApiClient.connect();
@Override
public void onConnected(@Nullable Bundle bundle)
locationListener = new LocationListener()
@Override
public void onLocationChanged(Location location)
gotoLocation(location.getLatitude(), location.getLongitude(), 15);
;
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(5000);
locationRequest.setFastestInterval(1000);
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, locationListener);
@Override
public void onConnectionSuspended(int i)
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 0)
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED &&
grantResults[1] == PackageManager.PERMISSION_GRANTED && grantResults[2] == PackageManager.PERMISSION_GRANTED)
showLocation();
public void showLocation()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
if (getActivity().checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& getActivity().checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
)
requestPermissions(new String[]android.Manifest.permission.ACCESS_COARSE_LOCATION,
android.Manifest.permission.ACCESS_FINE_LOCATION,
, 0);
else
Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if (location == null)
Toast.makeText(getActivity(), "Failed", Toast.LENGTH_LONG).show();
else
Toast.makeText(getActivity(), "Failed", Toast.LENGTH_LONG).show();
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 15);
m_map.animateCamera(cameraUpdate);
else
Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if (location == null)
Toast.makeText(getActivity(), "Failed", Toast.LENGTH_LONG).show();
else
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 15);
m_map.animateCamera(cameraUpdate);
public void gotoLocation(double lat, double lng, float zoom)
LatLng latLng = new LatLng(lat, lng);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, zoom);
m_map.moveCamera(cameraUpdate);
if(marker!=null)
marker.remove();
MarkerOptions markerOptions=new MarkerOptions()
.position(new LatLng(lat,lng))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.bg));
//dont put image in mipmap for marker
marker= m_map.addMarker(markerOptions);
@Override
public void onPause()
super.onPause();
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, locationListener);
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context="sawant.pritish.com.sharingfoodandhappiness.MapView.MapView">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_
android:layout_
/>
</FrameLayout>
我已经添加了所有必要的权限,例如互联网、访问精细和粗略的位置以及访问网络状态。我已经添加了元数据标签并使用了功能标签。有人可以帮忙吗。
【问题讨论】:
【参考方案1】:您可以通过更简单的解决方案来实现:只需调用
googleMap.setMyLocationEnabled(true);
在android.Manifest.permission.ACCESS_COARSE_LOCATION
或android.Manifest.permission.ACCESS_FINE_LOCATION
授予权限后的onMapReady(GoogleMap googleMap)
回调中。类似的东西:
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback
private static final int LOCATION_PERMISSION_REQUEST_CODE = 101;
private GoogleMap mGoogleMap;
private MapFragment mMapFragment;
private void makeLocationPermissionRequest()
ActivityCompat.requestPermissions(this,
new String[]android.Manifest.permission.ACCESS_FINE_LOCATION, LOCATION_PERMISSION_REQUEST_CODE);
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map_fragment);
mMapFragment.getMapAsync(this);
private void showCurrentPositionMarker()
mGoogleMap.setMyLocationEnabled(true);
@Override
public void onMapReady(GoogleMap googleMap)
mGoogleMap = googleMap;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
int locationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
if (locationPermission != PackageManager.PERMISSION_GRANTED)
makeLocationPermissionRequest();
else
showCurrentPositionMarker();
else
showCurrentPositionMarker();
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
switch (requestCode)
case LOCATION_PERMISSION_REQUEST_CODE:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
showCurrentPositionMarker();
else
return;
【讨论】:
以上是关于地图不显示带有标记的当前位置的主要内容,如果未能解决你的问题,请参考以下文章