0

我想在单击按钮时启动谷歌地图,然后用户将选择地点,我将获得用户选择的用户地址名称和经度和纬度。

我在这里和谷歌上搜索了很多,但没有找到答案。

我想要这样的东西:

Button getLocation= (Button) footer.findViewById(R.id.getLocation);
getLocation.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        //start google maps and let the user choose a place, then return to this
        //activity and get the latitude and longitude and the name of place
        //that was chosen
    }
});

这是我使用的地图片段:

 <fragment
            android:id="@+id/map"
            class="com.google.android.gms.maps.MapFragment"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginRight="150dp" />
4

1 回答 1

0

coords[]是一个全局double数组变量。

基本上,这个想法是在活动内的地图片段中显示地图。

我为标记设置了一个侦听器,因此当用户移动标记时,它将由handler.

R.id.map如果您按照本教程进行设置,则为片段布局。

    public class Activity extends FragmentActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.setContentView(R.layout.activity_layout);

                .......

            //Set up map view
            map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

            mHandler = new LocationHandler(this); 
                .......

        OnMarkerDragListener markerDragListener = new OnMarkerDragListener() {

                @Override
                public void onMarkerDragStart(Marker marker) {
                    // Called when the marker drag is started

                }

                @Override
                public void onMarkerDragEnd(Marker marker) {
                    // Called when the marker is dropped down.
                    coords[0] = marker.getPosition().latitude;
                    coords[1] = marker.getPosition().longitude;
                    RestoreUIwithSavedLocation(coords);
                    Log.d(TAG, "Pin Dropped at: " + coords[0] + ", " + coords[1]);
                }

                @Override
                public void onMarkerDrag(Marker marker) {

                }
            };

            map.setOnMarkerDragListener(markerDragListener);
        }
    }

    private void RestoreUIwithSavedLocation(double[] coordsArray) {
        Message.obtain(mHandler, LOAD_COORD, coordsArray).sendToTarget();
    }

    // Write a inner class
    static class LocationHandler extends Handler {
        WeakReference<Activity> mActivity;

        public LocationHandler(Activity activity) {
            mActivity = new WeakReference<Activity>(activity);
        }

        public void handleMessage(Message msg) {
            Activity contextActivity = mActivity.get();
            switch ((int)msg.what) {

            case LOAD_COORD:
                map.clear();
                double[] coordArray = (double[])msg.obj;
                Marker marker = map.addMarker(new MarkerOptions().position(new LatLng(coordArray[0], coordArray[1])));
                map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(coordArray[0], coordArray[1]), 18));
                map.animateCamera(CameraUpdateFactory.zoomTo(18), 2000, null);
                marker.setDraggable(true);
                String s = Double.toString(coordArray[0]) + ", " + Double.toString(coordArray[1]);              
                contextActivity.text_location.setText(s);
                break;

            }
        }
    }
}

更多谷歌地图API的参数可以参考这个:谷歌地图Android API v2 - GoogleMap.OnMarkerDragListener

于 2013-03-28T12:36:21.663 回答