0

到目前为止,我已经弄清楚如何通过点击在屏幕上获取标记,它显示经度和纬度数字,我希望能够将标记保存在 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;
}

}

4

1 回答 1

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 没有这样的方法来获取添加的标记(也许我错了)。

希望这可以帮助。

于 2013-01-27T23:00:48.237 回答