简体   繁体   中英

How to set marker in center of google map in android?

I am Building an android application where user select there favorite address using google map.

Now I had implement my Google map as Always the last value of center position is fixed in Lat,Long of google map.

Now I need an center marker with it that display the user there selected Location in map.

Here is my code -

@Override
public void onTouchDown(MotionEvent event) {
    mDownCameraPosition = getMap().getMap().getCameraPosition();
}

@Override
public void onTouchUp(MotionEvent event) {
    mUpCameraPosition = getMap().getMap().getCameraPosition();
    updateCameraPositionData();
}

private void updateCameraPositionData() {
    mDownCenterLat.setText(String
            .valueOf(mDownCameraPosition.target.latitude));
    mDownCenterLong.setText(String
            .valueOf(mDownCameraPosition.target.longitude));
    mDownZoom.setText(String.valueOf(mDownCameraPosition.zoom));
    mUpCenterLat.setText(String.valueOf(mUpCameraPosition.target.latitude));
    mUpCenterLong.setText(String
            .valueOf(mUpCameraPosition.target.longitude));
    mUpZoom.setText(String.valueOf(mUpCameraPosition.zoom));

}

private SupportMapFragment getMap() {
    return ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map));
}

Please help me....

You can change your onTouchUp() method like that:

    @Override
        public void onTouchUp(MotionEvent event) {
            mUpCameraPosition = getMap().getMap().getCameraPosition();
            getMap().getMap().clear();// to remove previous marker
            MarkerOptions options = new MarkerOptions() 
                                    .title(/*YOUR TITLE*/) 
                                    .position(new LatLng(mUpCameraPosition.target.latitude, mUpCameraPosition.target.longitude)) 
                                    .icon(BitmapDescriptorFactory.fromResource(R.drawable./*YOUR IMAGE*/)); 
            getMap().getMap().addMarker(options)
            updateCameraPositionData();
        }

PS: For better performance, if you use the same resource icon, you can add a BitmapDescriptor variable in your class to use it instead of creating it each time in "BitmapDescriptorFactory.fromResource(R.drawable./ YOUR IMAGE /)"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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