当前位置: 首页 > 知识库问答 >
问题:

Android,Mapbox,位置,Java

胡博艺
2023-03-14

我已经使用Mapbox创建了一个Android地图。我的地图显示了我在activity_main.xml中设置的位置。当我点击一个按钮时,系统会找到“当前位置”,并在该位置成功添加标记。但这里的问题是,我的地图实际上并没有自动移动到当前位置。我如何做到这一点?它总是显示我在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/.");
    }

}
}

共有1个答案

章涵蓄
2023-03-14

我不确定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:世界

5:陆地/大陆

10:城市

15:街道

20:建筑

 类似资料:
  • 本文向大家介绍mapbox 安装或设置,包括了mapbox 安装或设置的使用技巧和注意事项,需要的朋友参考一下 示例 有关设置或安装mapbox的详细说明。

  • 我正试图找到设备的位置。不幸的是,我的代码继续生成异常,但我无法找出是什么造成了问题。为了得到这个职位,我遵循了上面写的内容https://developer.android.com/training/location/retrieve-current.html#play-服务 谁能给我一些建议吗? 这是我的代码: 这是我在执行过程中得到的错误: 致命异常:主进程:com。菲利波卡拉布雷斯。其中,

  • 我举了一个例子:https://docs.mapbox.com/android/maps/examples/marker-symbol-layer/ 它起作用了。 然后,我尝试用源geojson替换这些点,但没有成功,如下所述:https://docs.mapbox.com/android/maps/guides/data-driven-styling/#geojson 除了在地图上添加新东西的部

  • 作为一名用户,我想使用Android应用程序使用路线在地图上导航。一旦我开始导航,我会开发其他方法来组织导航,但是到目前为止,我还不能在地图上显示路线。 我正在使用Mapbox自己提供的教程:https://www.mapbox.com/android-sdk/examples/directions/。没有消息错误,事实上,显示路线距离的toast消息在我访问地图视图后可见<代码>(“路线0.0米

  • 问题内容: 好的,所以我对Mapbox还是很陌生,在此之前我曾经使用过GMaps,但是我发现Mapbox能够满足我的需要,问题是我碰壁了。 我使用了他们网站上可用示例的组合 https://www.mapbox.com/android- sdk/examples/geocoding 和 https://www.mapbox.com/android- sdk/examples/directions

  • MapBox 是 iOS 上 MapKit 的一个开源实现。