如何使用笔记在sqlite数据库中保存地图标记?

时间:2013-01-27 22:41:41

标签: android google-maps-markers android-maps-v2

到目前为止,我已经弄清楚如何通过点击在屏幕上显示标记,并显示经度和纬度数字,我希望能够在SQL lite数据库中保存标记,并在标记上添加注释,因此,当加载地图并且标记加载了地图时,当用户点击标记信息时,将显示标记上方。到目前为止,这是我的地图类:

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

}

1 个答案:

答案 0 :(得分:1)

您需要在地图上单击侦听器(或在LongClick侦听器上),如下所示:

map.setOnMapClickListener(new OnMapClickListener() {

        @Override
        public void onMapClick(LatLng point) {

        }
    });

在该听众中,您将在地图上获得LatLng点击位置。然后你将创建这样的标记:

map.addMarker(new MarkerOptions()
        .position(point)
        .title("title")
        .snippet("snippet")
        .icon(icon));

为了保存标记,只需使用List或HashMap,因为map没有这样的方法来获取添加的标记(也许我错了)。

希望这有帮助。