如何在标记下移动地图?安卓 [英] How to move map under a marker? android

查看:106
本文介绍了如何在标记下移动地图?安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现地图的拖动效果.当我们拖动地图时,标记应显示当前位置或纬度和经度.在Uber应用程序中完成的操作如下:

I want to implement the drag effect of a map. When we drag the map the marker should show the current position or latitude and longitude. Its done in Uber application see below:

在此标记中,显示了拖动地图时的当前位置.我该如何实现??

In this marker shows the current location as the map is dragged. How can I achieve this???

ChooseFromMapActivity

ChooseFromMapActivity

    public class ChooseFromMapActivity extends AppCompatActivity implements GoogleMap.OnCameraChangeListener {

    TextView textShowAddress;
    private GoogleMap mMap;
    LatLng latLng;
    double latitude = 17.385044;
    double longitude = 78.486671;
    float zoom;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_from_map);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mMap = mapFragment.getMap();
        mMap.setMyLocationEnabled(true);
        LatLng center = mMap.getCameraPosition().target;
        mMap.getUiSettings().setZoomControlsEnabled(true);
        mMap.getUiSettings().setRotateGesturesEnabled(true);
        mMap.getUiSettings().setScrollGesturesEnabled(true);
        latLng = new LatLng(-34, 151);

     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

        textShowAddress = (TextView) findViewById(R.id.textShowAddress);
        mMap.addMarker(new MarkerOptions()
                .position(latLng)
                .draggable(false)
                .title("sent location :)"))
        .setDraggable(false);
        }
    }
}

推荐答案

有些人很困惑如何解决此问题,这是简单的解决方案 第1步:使用Google地图框架和中心的一个图像视图创建布局

Some people are confused how to solve this here is the simple solution step 1: create a layout with google map frame and one image view in the center

    <FrameLayout
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/btn_save" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/pin" /> 

第2步:创建方法初始化地图并将其放入onCreate()...意味着在活动开始时将初始化地图框架...在这里您需要创建SupportMapFragment的实例

Step 2 : Create method initialize map and put it in onCreate()... means in the starting of the activity so it will initialize the map frame...here you need to create an instance of SupportMapFragment

/*==================== initialize map fragment  ==================*/


private void initilizeMap() {

        mSupportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map_profile);

        if (mSupportMapFragment == null) {
            FragmentManager fragmentManager = getChildFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            mSupportMapFragment = SupportMapFragment.newInstance();
            fragmentTransaction.replace(R.id.map_profile, mSupportMapFragment).commit();
        }

        Log.d("mSupportMapFragment", "------" + mSupportMapFragment + "-----googleMap---" + googleMap);
    }

Step3:现在创建LatLng(私有LatLng latlng)的实例(它将返回地图的中心纬度和经度,以及我们在中心也拥有它的图像,因此好像我们拥有的标记图像返回纬度和经度

Step3 : Now Create an Instance of LatLng (private LatLng latlng) (It will return the centered latitude and longitude of your map and the image which we have it also in the center so it seems like the marker image which we have is returning the latitude and langitude )

第4步:创建一个方法IniLizeMap();这将在地图中设置属性和位置.

Step 4: Create a method IniLizeMap(); which will set the attributes and loation in to map.

public void IniLizeMap() {
        try {
            mSupportMapFragment.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(GoogleMap googleMap) {

                    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

                    // Showing / hiding your current location
                    googleMap.setMyLocationEnabled(false);

                    // Enable / Disable zooming controls
                    googleMap.getUiSettings().setZoomControlsEnabled(false);

                    // Enable / Disable my location button
                    googleMap.getUiSettings().setMyLocationButtonEnabled(false);

                    // Enable / Disable Compass icon
                    googleMap.getUiSettings().setCompassEnabled(false);

                    // Enable / Disable Rotate gesture`enter code here`
                    googleMap.getUiSettings().setRotateGesturesEnabled(false);

                    // Enable / Disable zooming functionality
                    googleMap.getUiSettings().setZoomGesturesEnabled(false);   

                    googleMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
                        @Override
                        public void onCameraChange(CameraPosition cameraPosition) {
                            latLng = cameraPosition.target;
                            googleMap.clear();
                            try {
                                getAddress(latLng.latitude, latLng.longitude);
                                Log.e("Changing address", getAddress(latLng.latitude, latLng.longitude));
                                Log.e("Latitude", latLng.latitude + "");
                                Log.e("Longitude", latLng.longitude + "");
                                String lat = latLng.latitude + "";
                                String lng = latLng.longitude + "";
                                String location = getAddress(latLng.latitude, latLng.longitude);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });                 



                    CameraPosition cameraPosition = new CameraPosition.Builder()
                            .target(new LatLng(Double.parseDouble(Utils.lat),
                                    Double.parseDouble(Utils.lng))).zoom(14).build();

                    googleMap.animateCamera(CameraUpdateFactory
                            .newCameraPosition(cameraPosition));
                }


            });

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

在此方法中,我将setOnCameraChangeListener应用于googlemap,这是我们问题的主要解决方案,它将在移动摄像机时返回居中位置,现在,如果您想在移动摄像机时获取地址,则创建方法getAddress即可返回移动地图时的字符串地址,所以这里是

In this method i apply setOnCameraChangeListener to googlemap that is the mian core solution of our problem which will return the centered location while moving the camera and now if you want to get the addres while it moving then create the method getAddress which will return string address while moving the map so here it is

第5步:创建方法getAddress(double lat,double lng),需要两个dubbles

Step 5: create method getAddress(double lat,double lng) which required two dubbles

 public String getAddress(double latitude, double longitude) {
        StringBuilder result = new StringBuilder();
        try {

            System.out.println("get address");
            Geocoder geocoder = new Geocoder(this, Locale.getDefault());
            List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
            if (addresses.size() > 0) {
                System.out.println("size====" + addresses.size());
                Address address = addresses.get(0);

                for (int i = 0; i <= addresses.get(0).getMaxAddressLineIndex(); i++) {
                    if (i == addresses.get(0).getMaxAddressLineIndex()) {
                        result.append(addresses.get(0).getAddressLine(i));
                    } else {
                        result.append(addresses.get(0).getAddressLine(i) + ",");
                    }
                }
                System.out.println("ad==" + address);
                System.out.println("result---" + result.toString());

                autoComplete_location.setText(result.toString()); // Here is you AutoCompleteTextView where you want to set your string address (You can remove it if you not need it)
            }
        } catch (IOException e) {
            Log.e("tag", e.getMessage());
        }

        return result.toString();
    }

====================就是这样,您就像UBER准备就绪一样映射============

==================== That's it You map like UBER is ready =============

这篇关于如何在标记下移动地图?安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆