Closed prensmiskin closed 4 years ago
Thanks for using Mapbox, @prensmiskin
Please be careful about posting any type of key (MAPKIT_API_KEY
) to a public repo. You might want to rotate that key now that it's been posted.
Please see https://docs.mapbox.com/android/maps/examples/show-a-users-location-on-a-fragment/ if you're trying to show the device location puck (the Maps SDK's LocationComponent
) in a fragment.
This repo's tickets are for issues with the demo app. For future Mapbox implementation questions, please contact our great Support team via https://support.mapbox.com/hc/en-us/requests/new?ticket_form_id=360000291231.
Thank you very much. Thanks to you.
package com.one.qaroco;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.mapbox.android.core.permissions.PermissionsListener;
import com.mapbox.android.core.permissions.PermissionsManager;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import android.widget.Toast;
import com.mapbox.android.core.permissions.PermissionsListener;
import com.mapbox.android.core.permissions.PermissionsManager;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.location.LocationComponent;
import com.mapbox.mapboxsdk.location.LocationComponentActivationOptions;
import com.mapbox.mapboxsdk.location.modes.CameraMode;
import com.mapbox.mapboxsdk.location.modes.RenderMode;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.MapboxMapOptions;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.mapboxsdk.maps.SupportMapFragment;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
public class FragmentBirinci extends Fragment implements PermissionsListener {
private MapboxMap mapboxMap;
private PermissionsManager permissionsManager;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_birinci_layout, container, false);
// Mapbox access token is configured here. This needs to be called either in your application
// object or in the same activity which contains the mapview.
Mapbox.getInstance(getActivity(), getString(R.string.access_token));
// Create supportMapFragment
SupportMapFragment mapFragment;
if (savedInstanceState == null) {
// Create fragment
final FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Build a Mapbox map
MapboxMapOptions options = MapboxMapOptions.createFromAttributes(getActivity(), null);
options.camera(new CameraPosition.Builder()
.target(new LatLng(38.899895, -77.03401))
.zoom(9)
.build());
// Create map fragment
mapFragment = SupportMapFragment.newInstance(options);
// Add map fragment to parent container
transaction.add(R.id.location_frag_container, mapFragment, "com.mapbox.map");
transaction.commit();
} else {
mapFragment = (SupportMapFragment) getFragmentManager().findFragmentByTag("com.mapbox.map");
}
if (mapFragment != null) {
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull MapboxMap mapboxMap) {
FragmentBirinci.this.mapboxMap = mapboxMap;
mapboxMap.setStyle(Style.OUTDOORS, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
enableLocationComponent(style);
}
});
}
});
}
return rootView;
}
@SuppressWarnings( {"MissingPermission"})
private void enableLocationComponent(@NonNull Style loadedMapStyle) {
// Check if permissions are enabled and if not request
if (PermissionsManager.areLocationPermissionsGranted(getActivity())) {
// Get an instance of the LocationComponent.
LocationComponent locationComponent = mapboxMap.getLocationComponent();
// Activate the LocationComponent
locationComponent.activateLocationComponent(
LocationComponentActivationOptions.builder(getActivity(), loadedMapStyle).build());
// Enable the LocationComponent so that it's actually visible on the map
locationComponent.setLocationComponentEnabled(true);
// Set the LocationComponent's camera mode
locationComponent.setCameraMode(CameraMode.TRACKING);
// Set the LocationComponent's render mode
locationComponent.setRenderMode(RenderMode.NORMAL);
} else {
permissionsManager = new PermissionsManager(this);
permissionsManager.requestLocationPermissions(getActivity());
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
permissionsManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
public void onExplanationNeeded(List<String> permissionsToExplain) {
Toast.makeText(getActivity(), R.string.user_location_permission_explanation, Toast.LENGTH_LONG).show();
}
@Override
public void onPermissionResult(boolean granted) {
if (granted) {
mapboxMap.getStyle(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
enableLocationComponent(style);
}
});
} else {
Toast.makeText(getActivity(), R.string.user_location_permission_not_granted, Toast.LENGTH_LONG).show();
getActivity().finish();
}
}
}
I like mabbox. It is both cheaper and more useful than google map.
Looks good, @prensmiskin ! We're glad that you're enjoying Mapbox 😄
Why can't I get location information? When I extend my class with AppCompatActivity, I can get a location. But when I extent with fragment, I cannot get a location.
I need to show a "mapbox" map in my fragment in the navigation drawer. I couldn't find a sample code for this.