Android,Mapbox,位置,Java
Android, Mapbox, location, Java
我使用 Mapbox 创建了 Android 地图。我的地图显示了我在 activity_main.xml 中设置的位置。当我点击一个按钮时,系统找到 'current location' 并在该位置成功添加标记。但这里的问题是,我的地图实际上并没有自动移动到当前位置。我该怎么做才能做到这一点?它总是
显示我在 activity_main.xml.
中设置的位置
代码
public class MainActivity extends Activity implements ActivityCompat.OnRequestPermissionsResultCallback {
private MapView mapView;
private MapboxMap map; //right now your map variable is null. In getMapAsync, you need to initialize map = mapboxMap;
Button myButton;
protected LocationManager locationManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MapboxAccountManager.start(this, "myToken");
final boolean permissionGranted = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.mapview);
myButton = (Button) findViewById(R.id.locate1);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(MapboxMap mapboxMap) {
Log.i("MapAsync", " is called");
//you need to initialize 'map' with 'mapboxMap';
map = mapboxMap;
}
});
final GPSTracker gps = new GPSTracker(MainActivity.this);
// On click on the 'Locate' button should add a 'new marker on map' with current location lat long
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (permissionGranted){
// check if GPS is enabled
if (gps.canGetLocation()) {
gps.location = gps.getLocation();
final double latitude = gps.getLatitude();
final double longitude = gps.getLongitude();
map.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title("Hello user !")
.snippet("Welcome to mapbox"));
//Adding the camera here as suggeted
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(gps.location.getLatitude(), gps.location.getLongitude())) // Sets the center of the map to Mountain View
.zoom(12) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
// \n is for new line
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
} else {
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
}else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 200);
}
}
});
}
// Add the mapView lifecycle to the activity's lifecycle methods
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
public class TelemetryServiceNotConfiguredException extends RuntimeException {
public TelemetryServiceNotConfiguredException() {
super("\nTelemetryService is not configured in your applications AndroidManifest.xml. " +
"\nPlease add \"com.mapbox.mapboxsdk.telemetry.TelemetryService\" service in your applications AndroidManifest.xml" +
"\nFor an example visit For more information visit https://www.mapbox.com/android-sdk/.");
}
}
}
我不确定 Mapbox 是否使用与 GoogleMap 相同的方法名称。如果是这样,您可以在成功添加标记后尝试添加 map.moveCamera(CameraUpdateFactory.newLatLngZoom(YourLatLng, 15))
。您也可以使用
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(YourLatLng) // Sets the center of the map to Mountain View
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
为相机设置动画。
注意:15是缩放级别。
缩放级别
1: World
5: Landmass/continent
10: City
15: Streets
20: Buildings
我使用 Mapbox 创建了 Android 地图。我的地图显示了我在 activity_main.xml 中设置的位置。当我点击一个按钮时,系统找到 'current location' 并在该位置成功添加标记。但这里的问题是,我的地图实际上并没有自动移动到当前位置。我该怎么做才能做到这一点?它总是 显示我在 activity_main.xml.
中设置的位置代码
public class MainActivity extends Activity implements ActivityCompat.OnRequestPermissionsResultCallback {
private MapView mapView;
private MapboxMap map; //right now your map variable is null. In getMapAsync, you need to initialize map = mapboxMap;
Button myButton;
protected LocationManager locationManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MapboxAccountManager.start(this, "myToken");
final boolean permissionGranted = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.mapview);
myButton = (Button) findViewById(R.id.locate1);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(MapboxMap mapboxMap) {
Log.i("MapAsync", " is called");
//you need to initialize 'map' with 'mapboxMap';
map = mapboxMap;
}
});
final GPSTracker gps = new GPSTracker(MainActivity.this);
// On click on the 'Locate' button should add a 'new marker on map' with current location lat long
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (permissionGranted){
// check if GPS is enabled
if (gps.canGetLocation()) {
gps.location = gps.getLocation();
final double latitude = gps.getLatitude();
final double longitude = gps.getLongitude();
map.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title("Hello user !")
.snippet("Welcome to mapbox"));
//Adding the camera here as suggeted
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(gps.location.getLatitude(), gps.location.getLongitude())) // Sets the center of the map to Mountain View
.zoom(12) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
// \n is for new line
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
} else {
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
}else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 200);
}
}
});
}
// Add the mapView lifecycle to the activity's lifecycle methods
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
public class TelemetryServiceNotConfiguredException extends RuntimeException {
public TelemetryServiceNotConfiguredException() {
super("\nTelemetryService is not configured in your applications AndroidManifest.xml. " +
"\nPlease add \"com.mapbox.mapboxsdk.telemetry.TelemetryService\" service in your applications AndroidManifest.xml" +
"\nFor an example visit For more information visit https://www.mapbox.com/android-sdk/.");
}
}
}
我不确定 Mapbox 是否使用与 GoogleMap 相同的方法名称。如果是这样,您可以在成功添加标记后尝试添加 map.moveCamera(CameraUpdateFactory.newLatLngZoom(YourLatLng, 15))
。您也可以使用
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(YourLatLng) // Sets the center of the map to Mountain View
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
为相机设置动画。
注意:15是缩放级别。
缩放级别
1: World
5: Landmass/continent
10: City
15: Streets
20: Buildings