Question

This is my TrackMap.java

public class TrackMap extends FragmentActivity implements LocationListener {

    GoogleMap map;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_track_map);

        boolean lowPowerMoreImportantThanAccurancy = true;
        LocationRequest request = LocationRequest.create();
        request.setPriority(lowPowerMoreImportantThanAccurancy ? 
                LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY: 
                LocationRequest.PRIORITY_HIGH_ACCURACY);

        map = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();

        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);

        // Enabling MyLocation Layer of Google Map
        map.setMyLocationEnabled(true);

        Marker marker = map.addMarker(new MarkerOptions()
                .position(new LatLng(0, 0)).draggable(true)
                // Set Opacity specified as a float between 0.0 and 1.0,
                // where 0 is fully transparent and 1 is fully opaque.
                .alpha(0.7f).flat(true));

    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

        map.clear();

        MarkerOptions mp = new MarkerOptions();

        mp.position(new LatLng(location.getLatitude(), location.getLongitude()));
        mp.title("Current Position");

        map.addMarker(mp);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
    }

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

    }

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

    }


}

My question here is how should I do to let user add marker on maps and my TrackMap.java able to get the coordinate and save in a variable. How should I do that?

From what I search online , they just teach markers with known coordinates. But now I would like to let user to add marker without knowing the coordinates.

Was it helpful?

Solution

What about adding a marker at where the user touches on the map? Here is the code:

map.setOnMapClickListener(new OnMapClickListener() {

    @Override
    public void onMapClick(LatLng point) {
          map.addMarker(new MarkerOptions().position(point));
          // you can get latitude and longitude also from 'point'
          // and using Geocoder, the address
         }
});

OTHER TIPS

You can use geocoding for that. Geocoding is the process of transforming a street address or other description of a location into a (latitude, longitude) coordinate. And once you get the coordinate you can add the marker there.

There can be a few steps in doing so:-

  1. Give a button to the user to mark his/her current location.
  2. OnClick of that button, get the current location and save it in your file/database etc.
  3. Add a marker on the map using the stored location and notify the user
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top