Reputation: 119
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.
Upvotes: 0
Views: 1841
Reputation: 5731
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
}
});
Upvotes: 1
Reputation: 1444
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.
Upvotes: 1
Reputation: 2877
There can be a few steps in doing so:-
Upvotes: 0