在这种情况下,地图会出现在我正在尝试制作的应用程序上,但是当应用程序打开时,文件夹不会出现在当前位置,而是出现在Sout Atlantic Ocean中。有没有办法在应用程序打开时调出当前位置?
这是我的代码:
public class GmapFragment extends Fragment implements OnMapReadyCallback,
ActivityCompat.OnRequestPermissionsResultCallback,
LocationListener
{
GoogleMap mMap;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
double longitude;
double latitude;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_gmaps, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MapFragment fragment = (MapFragment) getChildFragmentManager().findFragmentById(R.id.map);
fragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
LocationManager locManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
boolean network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Location location;
if(network_enabled){
location = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location!=null){
longitude = location.getLongitude();
latitude = location.getLatitude();
mMap.addMarker(new MarkerOptions()
.position(new LatLng(-7.0559935,110.4320226))
.title("Kos Munyiq"));
mMap.addMarker(new MarkerOptions()
.position(new LatLng(-7.0500666,110.4264657))
.title("Kos Pos"));
mMap.animateCamera(CameraUpdateFactory.zoomTo(0));
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setMapToolbarEnabled(false);
}
}
}
@Override
public void onLocationChanged(Location location) {
}
}
很抱歉,如果我的问题有问题。
答案 0 :(得分:0)
您正在某处添加标记。尝试使用以下代码而不是那些标记..
mMap.setMyLocationEnabled(true); <<this is the main line
mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location arg0) {
mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("It's Me!"));
}
});
即使您更改了位置,即时更改功能也会为您提供位置...如果您只需要当前位置,静态,则只需要以下代码,
mMap.setMyLocationEnabled(true);
mMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude,longitude))
.title("It's Me!"));
然后你会想要为相机设置动画,放大/缩小一点。 希望能帮助到你。干杯!