谷歌地图标记为输入类型的android

时间:2015-07-02 16:48:00

标签: android google-maps android-inputtype

我想知道我是否可以在谷歌地图中捕获用户输入。例如,如果我点击地图会有一个标记/标记,如果单击我的保存按钮,标记/标记将保存在我的数据库中?

1 个答案:

答案 0 :(得分:1)

您可以添加地图点击监听器,并在用户点击的点上放置标记:

 mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

        @Override
        public void onMapClick(LatLng point) {
            //save current location
            latLng = point;

            //remove previously placed Marker
            if (marker != null) {
                marker.remove();
            }

            //place marker where user just clicked
            marker = mMap.addMarker(new MarkerOptions().position(point).title("Marker")
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));

        }
    });

然后您可以添加一个按钮,在按钮单击监听器中,您将获得将当前标记中的数据保存到数据库的代码。

全班代码:

public class MapsActivity extends AppCompatActivity {

    private GoogleMap mMap;
    private Button saveButton;
    private LatLng latLng;
    private Marker marker;


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

        saveButton = (Button) findViewById(R.id.saveButton);

        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //check of user has placed a marker
                if (latLng != null){
                    double lat = latLng.latitude;
                    double lon = latLng.longitude;
                    //save location data in database
                    //...................

                    Toast.makeText(MapsActivity.this, "Location Saved: " + lat + " " + lon, Toast.LENGTH_LONG).show();
                }

            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {

        mMap.setMyLocationEnabled(true);
        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        mMap.getUiSettings().setMapToolbarEnabled(false);

        mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

            @Override
            public void onMapClick(LatLng point) {
                //save current location
                latLng = point;

                //remove previously placed Marker
                if (marker != null) {
                    marker.remove();
                }

                //place marker where user just clicked
                marker = mMap.addMarker(new MarkerOptions().position(point).title("Marker")
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));

            }
        });

    }
}

activity_maps.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapsActivity" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />


    <Button
        android:id="@+id/saveButton"
        android:text="Save Current Marker Position"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"/>
</RelativeLayout>

点击地图以放置标记,然后单击“保存”按钮后的结果:

enter image description here