谷歌地图v2标记位置

时间:2014-03-20 18:44:56

标签: android

在我创建的应用程序的一个活动中,我希望用户能够单击按钮,一旦单击,标记将被放置在当前位置。

setMarker.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    // Creating a LatLng object for the current location
                    LatLng latLng = new LatLng(location.getLatitude(),
                            location.getLongitude());
                    googleMap.addMarker(new MarkerOptions()
                    .position(LatLng)
                    .title("Hello world"));

2 个答案:

答案 0 :(得分:0)

声明一个像boolean putMarkerByButtonClick = false ;这样的全局布尔值并试试这个

setMarker.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            putMarkerByButtonClick = true  ;
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 400, 1000, this);
        }
    });

并在您的覆盖方法onLocationChange方法上写下:

@Override
public void onLocationChanged(Location location) {
    if(putMarkerByButtonClick ){
        putMarkerByButtonClick = false ; 
        map.addMarker(new MarkerOptions()
                .position(new LatLng(location.getLatitude(), location.getLongitude()))
                .title("Hello world"));
    }

}

答案 1 :(得分:0)

我发给你一些密码。阅读它们并适当使用它们。点击按钮

private void locateMe() {
    // Checking for GPS Enabled

    locationManager = (LocationManager) this
            .getSystemService(Context.LOCATION_SERVICE);

    // getting GPS status
    isGPSEnabled = locationManager
            .isProviderEnabled(LocationManager.GPS_PROVIDER);

    if (!isGPSEnabled) {
        // GPS is disabled

        askUserToEnableGPS();

    }

}

/**
 * 
 */
private void askUserToEnableGPS() {
    // Asking user to enable GPS

    // 1. Instantiate an AlertDialog.Builder with its constructor
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    // 2. Chain together various setter methods to set the dialog
    // characteristics
    builder.setMessage(R.string.generic_gps_not_found)
            .setTitle(R.string.generic_gps_not_found_message_title)
            .setPositiveButton(R.string.generic_yes,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            // User selected yes
                            Intent intent = new Intent(
                                    Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            startActivity(intent);
                        }
                    })
            .setNegativeButton(R.string.generic_no,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            // User selected no
                        }
                    });

    // 3. Get the AlertDialog from create()
    AlertDialog dialog = builder.create();
    dialog.show();

}



LocationListener listener = new LocationListener() {

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onLocationChanged(Location arg0) {
        // Setting the marker

        if (googleMap == null || location == null) {
            return;
        } else {

            googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(
                    location.getLatitude(), location.getLongitude())));

            final Handler handler1 = new Handler();
            handler1.postDelayed(new Runnable() {
                @Override
                public void run() {
                    // Do something after 3000ms
                    googleMap.animateCamera(CameraUpdateFactory.zoomTo(17));

                }
            }, 1000);

            Marker myLocation = googleMap.addMarker(new MarkerOptions()
                    .position(
                            new LatLng(location.getLatitude(), location
                                    .getLongitude()))
                    .title("Me")
                    .snippet("I am here")
                    .icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.ic_launcher)));

        }

        locationManager.removeUpdates(listener);
        locationManager = null;

    }
};
相关问题