For a current project, I need to do something with latitude and longitude data.
I am using the Google maps api (not Maps app).
When the project was created, I choose to use a Google Maps Activity template. The project contains a MainActivity and a MapsActivity. (min. api 19)
I looking for a way to get the longitude and latitude from a location that the user touched on the map.
The longitude and the latitude from the selected location needs to be selected in MapsActivity, and then passed down to the MainActivity (with CONSTANTS or intents?) Finally, when the user selected the location and the data is passed successfully, the user gets back to the MainActivity.
How can I get the selected location on a MapsActivity using Google Maps API for Android, and pass that selected location back to MainActivity?
Use startActivityForResult()
in order to open up the MapsActivity, and then use a click listener on the Google Map in order to let the user choose a point.
Here is a simple example.
First, MainActivity, which opens up MapsActivity upon a button click, and shows a Toast with the lat/lon of the point that the user chooses in MapsActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_two);
Button button = (Button) findViewById(R.id.button_pick_point);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pickPointOnMap();
}
});
}
static final int PICK_MAP_POINT_REQUEST = 999; // The request code
private void pickPointOnMap() {
Intent pickPointIntent = new Intent(this, MapsActivity.class);
startActivityForResult(pickPointIntent, PICK_MAP_POINT_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_MAP_POINT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
LatLng latLng = (LatLng) data.getParcelableExtra("picked_point");
Toast.makeText(this, "Point Chosen: " + latLng.latitude + " " + latLng.longitude, Toast.LENGTH_LONG).show();
}
}
}
}
Then, MapsActivity, which uses a GoogleMap.OnMapClickListener
in order to get the lat/lon of the point that the user chose, and pass the data back to MainActivity. Note that the LatLng object can be passed as an extra since it implements the Parcelable interface.
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
Intent returnIntent = new Intent();
returnIntent.putExtra("picked_point",latLng);
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
});
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.