简体   繁体   English

如何始终将地图标记保持在屏幕中央? Android的

[英]How to keep the map marker in the center of the screen at all times? Android

I am trying to find a way to center my map marker in my map fragment activity. 我试图找到一种方法将地图标记居中在地图片段活动中。 So far, I have tried adding a marker to the center of the screen every time the map is moved ie, the camera position is updated. 到目前为止,每次尝试移动地图(即更新相机位置)时,我都尝试在屏幕中心添加一个标记。 But the problem with this solution is that every time a new marker gets added into the map at the center and the old one stays there so in just a couple of drags i would have like 10 markers on my screen. 但是此解决方案的问题在于,每次在中心位置的地图中添加一个新的标记,而旧的标记会停留在该位置,因此只需几次拖动,我就希望在屏幕上显示10个标记。 I tried using the clear method before adding the next marker but now the marker is just flashing too much. 在添加下一个标记之前,我尝试使用clear方法,但是现在标记闪烁太多。 Here's my code: 这是我的代码:

mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
            @Override
            public void onCameraChange(CameraPosition cameraPosition) {
                LatLng location=mMap.getCameraPosition().target;
                MarkerOptions marker=new MarkerOptions().position(location).title("");
                mMap.clear();
                mMap.addMarker(marker);
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 16));
            }
        });

The other solution i have found is to use a centered ImageView but the problem with that is that i want to change the icon of my marker. 我发现的另一个解决方案是使用居中的ImageView,但是问题是我想更改标记的图标。 But when i add an image image to the image view, the center of the camera is in the green little circle in the center of this image and not the pointy end of it like i want to. 但是,当我将图像添加到图像视图时,相机的中心在该图像中心的绿色小圆圈中,而不是我想要的尖端。 Please help. 请帮忙。

Don't use a real marker. 不要使用真实的标记。 Put the MapFragment or MapView in a layout with a fake marker image centered over it. MapFragmentMapView放置在布局中,并在其上方居中放置假标记图像。 When the location is chosen, place a real marker and hide the fake one. 选择位置后,放置一个真实的标记并隐藏假标记。

If the "pointy end" of the fake marker image is not in its exact center, simply pad the image with transparent space until it is. 如果假标记图像的“尖端”不在其确切中心,则只需在图像上填充透明空间,直到它位于中心为止。

Yep, actually you can use setOnCameraChangeListener. 是的,实际上您可以使用setOnCameraChangeListener。 So you don't need to clear your marker every time, just set new position for it. 因此,您无需每次都清除标记,只需为其设置新位置即可。 Try this code: 试试这个代码:

map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
    @Override
    public void onCameraChange(CameraPosition cameraPosition) 
    {
        if(marker==null)
        {
            marker = map.addMarker(new MarkerOptions().position(cameraPosition.target).title("Marker")
                    .icon(BitmapDescriptorFactory.fromResource(R.mipmap.marker)));
            marker.showInfoWindow();
        }
        else
        {
            marker.setPosition(cameraPosition.target);
        }
    }
});

This should work. 这应该工作。 So, the only problem is that your marker will move to centre of the map only at the end of map dragging. 因此,唯一的问题是您的标记仅在地图拖动结束时才会移动到地图的中心。 To avoid this problem you can just use something like ImageView and paste it at the centre of your map Fragment. 为避免此问题,您可以使用ImageView之类的东西并将其粘贴到地图片段的中心。 Thereafter you can use the code above with adding just: 之后,您可以使用上面的代码添加以下内容:

marker.setVisible(false); marker.setVisible(假);

Assuming you are using Google Play Services for getting user's location call below from 假设您正在使用Google Play服务从以下位置获取用户的位置呼叫

@Override
    public void onConnected(Bundle connectionHint) {
        Log.i(TAG, "Connected to GoogleApiClient");

        // If the initial location was never previously requested, we use
        // FusedLocationApi.getLastLocation() to get it. If it was previously requested, we store
        // its value in the Bundle and check for it in onCreate(). We
        // do not request it again unless the user specifically requests location updates by
        // pressing
        // the Start Updates button.
        //
        // Because we cache the value of the initial location in the Bundle, it means that if the
        // user launches the activity,
        // moves to a new location, and then changes the device orientation, the original location
        // is displayed as the activity is re-created.
        if (mCurrentLocation == null) {
            mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            drawOnMap(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()));
        }
    }

And

/**
     * Callback that fires when the location changes.
     */
    @Override
    public void onLocationChanged(Location location) {
        mCurrentLocation = location;
        if (location != null) {
            drawOnMap(new LatLng(location.getLatitude(), location.getLongitude()));
        }
    }

This will be the function to update marker on the map. 这将是更新地图上标记的功能。

   public void drawOnMap(final LatLng location) {
        if (isFirstLocation) {
            markerCurrent = mMap.addMarker(new MarkerOptions()
                    .position(location)
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.dot)));
            isFirstLocation = false;
        } else {
            markerCurrent.setPosition(location);
        }
        CameraPosition cp = getCameraPosition(location);
        int animationDuration = 600;
        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cp), animationDuration, null);
    }

First make a marker variable Then assign marker To add map marker 首先创建一个标记变量,然后分配标记以添加地图标记

LatLng myMapPosition = new LatLng(19.0760, 72.8777);
marker = mMap.addMarker(new MarkerOptions()
                .position(myMapPosition)
                .draggable(true)              
                .icon(BitmapDescriptorFactory.defaultMarker
                         (BitmapDescriptorFactory.HUE_RED))
                .title("My Location"));


mMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
            @Override
            public void onCameraMove() {
                LatLng midLatLng = mMap.getCameraPosition().target;
                if (marker!=null) marker.setPosition(midLatLng);
                else Log.d("TAG","Marker is null");
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM