Android 如何使用notes在sql lite数据库中保存地图标记?
到目前为止,我已经知道如何通过点击在屏幕上获得标记,它显示经度和纬度数字,我希望能够将标记保存在SQL lite数据库中,并在标记上添加注释,因此当地图加载且标记与地图一起加载时,当用户点击标记时,标记信息将显示在标记上方。以下是我迄今为止的地图课程:Android 如何使用notes在sql lite数据库中保存地图标记?,android,google-maps-markers,android-maps-v2,Android,Google Maps Markers,Android Maps V2,到目前为止,我已经知道如何通过点击在屏幕上获得标记,它显示经度和纬度数字,我希望能够将标记保存在SQL lite数据库中,并在标记上添加注释,因此当地图加载且标记与地图一起加载时,当用户点击标记时,标记信息将显示在标记上方。以下是我迄今为止的地图课程: public class Map extends FragmentActivity { GoogleMap googleMap; @Override protected void onCreate(Bundle savedInst
public class Map extends FragmentActivity {
GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
setUpMapIfNeeded();
SupportMapFragment supportMapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
// Getting a reference to the map
googleMap = supportMapFragment.getMap();
googleMap.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();
// Setting the position for the marker
markerOptions.position(latLng);
// Setting the title for the marker.
// This will be displayed on taping the marker
markerOptions.title(latLng.latitude + " : " + latLng.longitude);
// Clears the previously touched position
googleMap.clear();
// Animating to the touched position
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
// Placing a marker on the touched position
googleMap.addMarker(markerOptions);
}
});
}
@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 (googleMap == null) {
// Try to obtain the map from the SupportMapFragment.
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (googleMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
googleMap.setMyLocationEnabled(true);
googleMap.getMyLocation();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}您需要在map click listener(或LongClick listener)上设置如下:
map.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
}
});
map.addMarker(new MarkerOptions()
.position(point)
.title("title")
.snippet("snippet")
.icon(icon));
在该侦听器中,您将获得地图上单击位置的LatLng点。然后您将创建如下标记:
map.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
}
});
map.addMarker(new MarkerOptions()
.position(point)
.title("title")
.snippet("snippet")
.icon(icon));
为了保存标记,只需使用List或HashMap,因为map没有这样的方法来获取添加的标记(可能我错了)
希望这能有所帮助。谢谢,我将尝试您的代码,看看是否能让它正常工作!!你的回答只是我最终想到的一小部分,但它提供了一个良好的开端,接受悬赏。