google地图标记为输入类型android
我想知道是否可以在谷歌地图中捕获用户输入。例如,如果我单击地图,将有一个标记/标志,如果单击我的保存按钮,标记/标志将保存在我的数据库中 您可以添加地图单击侦听器,并在用户点击的点上放置一个标记:google地图标记为输入类型android,android,google-maps,android-inputtype,Android,Google Maps,Android Inputtype,我想知道是否可以在谷歌地图中捕获用户输入。例如,如果我单击地图,将有一个标记/标志,如果单击我的保存按钮,标记/标志将保存在我的数据库中 您可以添加地图单击侦听器,并在用户点击的点上放置一个标记: mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() { @Override public void onMapClick(LatLng point) { //save c
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>
点击地图放置标记,然后单击“保存”按钮后的结果:
您必须对谷歌地图API进行一些研究。然后回到这里,问你如何改进你所学的知识。你的问题有点宽泛,而且要求提供教程是离题的,所以我删除了你文章的这一部分。我本打算给你们一些其他SO帖子的链接,但我并没有找到任何真正规范的答案。因此,我继续并发布了关于你问题地图部分的答案。至于数据库功能,还有很多其他帖子可以帮助您。