Java 根据当前位置缩放标记位置

Java 根据当前位置缩放标记位置,java,android,google-maps,google-maps-api-3,Java,Android,Google Maps,Google Maps Api 3,我在安卓系统中使用谷歌地图。我一直在调整缩放级别 我在地图中心有一个标记,我使用mapController.setCenter(当前位置)设置该标记 我有10个或更多的标记以不同的颜色显示在地图上 预期结果:人员的当前位置应始终位于中间,我必须调整缩放级别,以便屏幕上显示绿色标记(当前位置标记位于中间) 我尝试的是: int greenlat=(int)(AppCoordinates.getInstance().latitudelist.get(0)*1E6); int greenlong=(i

我在安卓系统中使用谷歌地图。我一直在调整缩放级别

我在地图中心有一个标记,我使用mapController.setCenter(当前位置)设置该标记

我有10个或更多的标记以不同的颜色显示在地图上

预期结果:人员的当前位置应始终位于中间,我必须调整缩放级别,以便屏幕上显示绿色标记(当前位置标记位于中间)

我尝试的是:

int greenlat=(int)(AppCoordinates.getInstance().latitudelist.get(0)*1E6); int greenlong=(int)(AppCoordinates.getInstance().longitudelist.get(0)*1E6); int currentlat=(int)(AppCoordinates.getInstance().getCurrentLatitude()*1E6); int currentlong=(int)(AppCoordinates.getInstance().getcurrentlong()*1E6)


使用is,我可以在屏幕上显示标记,但我希望当前位置显示在屏幕的中心。

我的想法是操作LatLngBounds,因为它表示所有包含的标记,以便在地图上对齐的矩形中查看。所以,我们只需要识别所有构成当前位置的点,它们将在地图的中心被查看

  • 把所有的记号笔放进装订线
  • 将相机移动到当前位置作为中心点
  • 获取当前地图所有角的4个位置,并将其放在边界点列表中
  • 这是我的样本:

    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    Marker myLocation = mMap.addMarker(new MarkerOptions()
            .position(new LatLng(2.6,101.5))
            .title("My-Current-Location"));
    
    
    Marker addMarker1 = mMap.addMarker(new MarkerOptions()
                .position(new LatLng(2.6,101))
                .title("Hello world-1"));
    
    Marker addMarker2 = mMap.addMarker(new MarkerOptions()
                .position(new LatLng(2.7,101))
                .title("Hello world-2"));
    
    Marker addMarker3 = mMap.addMarker(new MarkerOptions()
                .position(new LatLng(3.7,105))
                .title("Hello world-3"));
    
    builder.include(myLocation.getPosition());
    builder.include(addMarker1.getPosition());
    builder.include(addMarker2.getPosition());
    builder.include(addMarker3.getPosition());
    
    //haha enough to use for loop for all your markers
    
    int padding = 100; // offset from edges of the map in pixels
    
    CameraUpdate cameraUpdate =CameraUpdateFactory.newLatLngBounds(builder.build(), padding);
    mMap.moveCamera(cameraUpdate);
    mMap.moveCamera(CameraUpdateFactory.newLatLng(myLocation.getPosition()));
    
    LatLng nrLatLng = mMap.getProjection().getVisibleRegion().nearRight;
    LatLng frLatLng = mMap.getProjection().getVisibleRegion().farRight;
    LatLng nlLatLng = mMap.getProjection().getVisibleRegion().nearLeft;
    LatLng flLatLng = mMap.getProjection().getVisibleRegion().farLeft;
    
    builder.include(nrLatLng);
    builder.include(frLatLng);
    builder.include(nlLatLng);
    builder.include(flLatLng);
    
    cameraUpdate =CameraUpdateFactory.newLatLngBounds(builder.build(), padding);
    mMap.animateCamera(cameraUpdate);
    

    试试这个方法map.animateCamera(CameraUpdateFactory.newLatLngZoom(newLatlng(currentlat,currentlong),8));8是缩放范围,可以根据您的要求在给定点内或外进行更改。嗨,Haresh,我使用的是android.maps,因此没有称为animateCamera()的方法:(谢谢zainoz…我将尝试此示例,希望对您有所帮助:)
    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    Marker myLocation = mMap.addMarker(new MarkerOptions()
            .position(new LatLng(2.6,101.5))
            .title("My-Current-Location"));
    
    
    Marker addMarker1 = mMap.addMarker(new MarkerOptions()
                .position(new LatLng(2.6,101))
                .title("Hello world-1"));
    
    Marker addMarker2 = mMap.addMarker(new MarkerOptions()
                .position(new LatLng(2.7,101))
                .title("Hello world-2"));
    
    Marker addMarker3 = mMap.addMarker(new MarkerOptions()
                .position(new LatLng(3.7,105))
                .title("Hello world-3"));
    
    builder.include(myLocation.getPosition());
    builder.include(addMarker1.getPosition());
    builder.include(addMarker2.getPosition());
    builder.include(addMarker3.getPosition());
    
    //haha enough to use for loop for all your markers
    
    int padding = 100; // offset from edges of the map in pixels
    
    CameraUpdate cameraUpdate =CameraUpdateFactory.newLatLngBounds(builder.build(), padding);
    mMap.moveCamera(cameraUpdate);
    mMap.moveCamera(CameraUpdateFactory.newLatLng(myLocation.getPosition()));
    
    LatLng nrLatLng = mMap.getProjection().getVisibleRegion().nearRight;
    LatLng frLatLng = mMap.getProjection().getVisibleRegion().farRight;
    LatLng nlLatLng = mMap.getProjection().getVisibleRegion().nearLeft;
    LatLng flLatLng = mMap.getProjection().getVisibleRegion().farLeft;
    
    builder.include(nrLatLng);
    builder.include(frLatLng);
    builder.include(nlLatLng);
    builder.include(flLatLng);
    
    cameraUpdate =CameraUpdateFactory.newLatLngBounds(builder.build(), padding);
    mMap.animateCamera(cameraUpdate);