用户触摸屏幕时如何更新标记?

时间:2014-03-29 10:13:30

标签: android android-maps-v2

public void pointLocation(){
    if(gmap!=null){
    gmap.setOnMapLongClickListener(new OnMapLongClickListener() {

        @Override
            public void onMapLongClick(LatLng latlng) {
                // TODO Auto-generated method stub
                double lat = latlng.latitude;
                double lng = latlng.longitude;
                mVisible=gmap.addMarker(new MarkerOptions().position(new LatLng(lat, lng)));
                Toast.makeText(getApplicationContext(), "Latitude :"+" "+lat+"Longitude :"+" "+lng, Toast.LENGTH_LONG).show();
            }
        });
    mVisible.setVisible(false);//marker setVisible off so that old marker get destroy and new get appear
    }
}

1 个答案:

答案 0 :(得分:1)

package com.sample;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Window;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.sample.R;

public class MainActivity extends FragmentActivity {

    GoogleMap gmap;
    // GPSTracker gpsTracker;
    SupportMapFragment mapFragment;
    Marker mVisible;

    boolean checkCall = true;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.selectlocation);

        // gpsTracker = new GPSTracker(this);
        mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.maplocation);

        userPosition();

        pointLocation();
    }

    public void userPosition() {
        gmap = mapFragment.getMap();
        if (gmap != null) {
            // if (gpsTracker.canGetLocation()) {
            // double latCurrentloc = gpsTracker.getLatitude();
            // double lngCurrentloc = gpsTracker.getLongitude();
            LatLng pointer = new LatLng(17.2145632, 8.2345876);
            gmap.addMarker(new MarkerOptions().position(pointer).title(
                    "Hello world"));
            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(pointer).zoom(12).build();
            gmap.animateCamera(CameraUpdateFactory
                    .newCameraPosition(cameraPosition));
            // }
        }
    }

    public void pointLocation() {
        if (gmap != null) {
            // mVisible.setVisible(true);

            gmap.setOnMapLongClickListener(new OnMapLongClickListener() {
                @Override
                public void onMapLongClick(LatLng latlng) {
                    if (checkCall) {

                        double lat = latlng.latitude;
                        double lng = latlng.longitude;
                        mVisible = gmap.addMarker(new MarkerOptions()
                                .position(new LatLng(lat, lng)));
                        Toast.makeText(
                                getApplicationContext(),
                                "Latitude :" + " " + lat + "Longitude :" + " "
                                        + lng, Toast.LENGTH_LONG).show();

                        checkCall = false;
                    }
                }
            });

        }
    }
}