java - Android : Maps not zooming in to my location, 沿 Y 坐标随机位置

标签 java android google-maps random

我正在开发一个 Android 项目,其中我正在研究 map 功能。在 map 中,我想在我周围的随机位置添加标记,这些标记随机分散,并且默认情况下 map 应放大到我当前的位置。目前,随机位置只是沿着 Y 轴添加,而不是均匀分散。而且缩放功能在我的位置不起作用。有什么想法吗?谢谢..

代码:

 SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
        googleMap = fm.getMap();

        // Getting GoogleMap object from the fragment

        googleMap.getUiSettings().setMapToolbarEnabled(true);
        googleMap.getUiSettings().setZoomControlsEnabled(true);
        // Enabling MyLocation Layer of Google Map
        googleMap.setMyLocationEnabled(true);
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(12), 5000, null);

        // Opening the sharedPreferences object
        sharedPreferences = getSharedPreferences("location", 0);

        // Getting number of locations already stored
        locationCount = sharedPreferences.getInt("locationCount", 0);


        // Getting stored zoom level if exists else return 0
        //zoom = sharedPreferences.getString("zoom", "12");

        gps = new GPSTracker(MapsActivity.this);
        double radius = 10;
        if (gps.canGetLocation()) {
            longitude = gps.getLongitude();
            latitude = gps.getLatitude();

            Double[] gpsArray = new Double[]{longitude,latitude};

            List<LatLng> randomPoints = new ArrayList<>();
            List<Float> randomDistances = new ArrayList<>();
            Location myLocation = new Location("");
            myLocation.setLatitude(latitude);
            myLocation.setLongitude(longitude);

            //This is to generate 10 random points
            for(int i = 0; i<10; i++) {
                double x0 = latitude;
                double y0 = longitude;

                Random random = new Random();

                // Convert radius from meters to degrees
                double radiusInDegrees = radius / 111000f;

                double u = random.nextDouble();
                double v = random.nextDouble();
                double w = radiusInDegrees * Math.sqrt(u);
                double t = 2 * Math.PI * v;
                double x = w * Math.cos(t);
                double y = w * Math.sin(t);

                // Adjust the x-coordinate for the shrinking of the east-west distances
                double new_x = x / Math.cos(y0);

                double foundLatitude = new_x + x0;
                double foundLongitude = y + y0;
                LatLng randomLatLng = new LatLng(foundLatitude, foundLongitude);
                randomPoints.add(randomLatLng);
                Location l1 = new Location("");
                l1.setLatitude(randomLatLng.latitude);
                l1.setLongitude(randomLatLng.longitude);
                randomDistances.add(l1.distanceTo(myLocation));
                Marker mrk = googleMap.addMarker(new MarkerOptions().position(randomLatLng).title("Title").snippet("Snippet"));

            }
            //Get nearest point to the centre
          //  int indexOfNearestPointToCentre = randomDistances.indexOf(Collections.min(randomDistances));
           // return randomPoints.get(indexOfNearestPointToCentre);


        } else {
            gps.showSettingsAlert();
        }

        googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                int restoId = Integer.valueOf(marker.getSnippet());

            return true;
            }
        });

mapsact.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <fragment  xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

   </RelativeLayout>

输出:

enter image description here

更新

代码:

 Location myLocation = new Location("");
                myLocation.setLatitude(latitude);
                myLocation.setLongitude(longitude);
                double x0 = latitude;
                double y0 = longitude;

                double x1 = x0;
                double y1 = y0;

                //This is to generate 10 random points
                for (int i = 0; i < 10; i++) {
 x0 = x0 + isOneOrZero();
                    y0 = y0 + isOneOrZero();
                    LatLng randomLatLng = new LatLng(x0, y0);
                    Marker mrk = googleMap.addMarker(new MarkerOptions().position(randomLatLng).title("Title").snippet("Snippet"));

                    x1 = x1 - isOneOrZero();
                    y1 = y1 - isOneOrZero();

                    LatLng randomLatLng1 = new LatLng(x1, y1);
                    Marker mrk1 = googleMap.addMarker(new MarkerOptions().position(randomLatLng).title("Title").snippet("Snippet"));
}


   public int isOneOrZero() {
        Random random = new Random();
        if (random.nextBoolean()) {
            return 1;
        } else {
            return 0;
        }
    }

最佳答案

试试这个:

//Calculate the markers to get their position
LatLngBounds.Builder b = new LatLngBounds.Builder();
for (Marker m : markers) {
    b.include(m.getPosition());
}
LatLngBounds bounds = b.build();
//Change the padding as per needed
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 10,10,10);
mMap.animateCamera(cu);

关于java - Android : Maps not zooming in to my location, 沿 Y 坐标随机位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43735121/

相关文章:

java - 使用 JUnit 在 Intellij 上运行特定测试

java - 阻止 jBoss/Seam 为我处理异常

javascript - Google Maps Api V3 - 可编辑信息窗口

google-maps - 在 googlemaps 上单击默认标记/地点时,如何捕获单击事件?

java - 无法为 Jar 中的文件创建文件对象

java - 运行 DataNucleus Enhancer 3.1.0-release 的 NullPointerException

android - 更改标签很慢/滞后 - 使用 fragment

android - 如何手动将 Gradle 模块添加到 Android 项目中?

java - 使用正则表达式验证可变长度字符串

css - 是否可以在不使用图像映射软件的情况下获取图像映射的坐标?