简体   繁体   English

谷歌地图android获取位置

[英]Google maps android get place

I want to start google maps when i click on button and then the user will choose place and I will get the user address name and longitude and latitude that the user choosed. 当我单击按钮时,我想启动google maps,然后用户将选择地点,我将获得用户选择的用户地址名称,经度和纬度。

I searched alot over here and on google and didnt find an answer. 我在这里和谷歌上搜索了很多,却没有找到答案。

I want something like this: 我想要这样的东西:

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
    }
});

this is the map fragment that i use: 这是我使用的地图片段:

 <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" />

coords[] is a global double array variable. coords[]是全局double数组变量。

Basically the idea is to show the map within a map fragment inside the activity. 基本上,想法是在活动内的地图片段中显示地图。

I set a listener to the marker, so when user move the marker, it will be triggered and handled by the handler . 我为标记设置了一个侦听器,因此当用户移动标记时,它将由handler触发并处理。

R.id.map is the fragment layout if you followed this tutorial to set up. 如果您按照本教程进行设置,则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;

            }
        }
    }
}

For more parameters of the Google Map API, you can refer to this: Google Maps Android API v2 - GoogleMap.OnMarkerDragListener 有关Google Map API的更多参数,您可以参考以下内容: Google Maps Android API v2-GoogleMap.OnMarkerDragListener

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

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