简体   繁体   中英

Opening maps via intent with preselected location

I'm trying to make an app that will use an Place autocomplete fragment in one activity and when I select a place in it, on a click of a button, I'm supposed to open another activity (Google Maps activity) and it's supposed to get the location that I chose in the first one. I've tried passing Longitude and Latitude from Main class to Maps class, but it just crashes my app. Anyone have any idea how to fix? Thank you in advance!

EDIT: Sample of what I've tried so far:

/*This is the main class where my autocomplete fragment is*/

Double Lat;
Double lang;

public void onPlaceSelected(Place place) {

// TODO: Get info about the selected place.
// Lat = place.getLatLng().latitude;
// lang = place.getLatLng().longitude;
setLang(place.getLatLng().longitude);
setLat(place.getLatLng().latitude);
Log.i(TAG, "Place: " + place.getName());
}
public void setLang(Double langg){
this.lang = langg;
}
public void setLat(Double Latt){
this.Lat = Latt;
}
public Double getLang(){
return lang;
}
public Double getLat(){
return Lat;
}

/* This is the maps class */

MainActivity maAct;
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng place = new LatLng(maAct.getLat(), maAct.getLang());
// LatLng paris = new LatLng(48.8566, 2.3522);
mMap.addMarker(new MarkerOptions().position(place).title("Marker inPlace")); 
mMap.moveCamera(CameraUpdateFactory.newLatLng(place));

}

What i understand is you want to navigate to the passed latitude and longitude.Here is a sample

String uri = String.format(Locale.ENGLISH, "google.navigation:q=%f,%f", Double.parseDouble(latitude), Double.parseDouble(longitude));
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
                    if(intent.resolveActivity(context.getPackageManager())!=null)
                        context.startActivity(intent);

You can find more info like creating markers and all here

Edit: As per your code you want to set the marker. Here is the steps and sample for a working marker example.

Ok, you may consider to pass the latitude and longitude by Intent.

// MainActivity
Intent intent = new Intent(MainActivity.this, MapActivity.class);
intent.putExtra("lat", Lat);
intent.putExtra("lng", lang);
startActivity(intent);

And..

// Map Activity onMapReady
double lat = getIntent().getDoubleExtra("lat", DEFAULT_LAT);
double lng = getIntent().getDoubleExtra("lng", DEFAULT_LNG);
LatLng place = new LatLng(lat, lng);
mMap.addMarker(new MarkerOptions().position(place).title("Marker inPlace")); 

You cannot getValue by maAct, your concept is incorrect.

First if you just

MainActivity maAct;

maAct is not assigned any 'value', so that if you call any methods of maAct, you will get NullPointerException.

Besides, if you really give it a 'value',

// it is totally wrong, just an example only
MainActivity maAct = new MainActivity();

But it is not the same reference so that you cannot get the values you stored before.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM