I am trying to pass the latitude and Longitude from the onLocationChanged in the MainActivity to another packagecom.route.provider
classDataPrivider
b
You can add the values directly into intent.
intent.putExtra(KEY, VALUE)
otherwise try using
args.putDouble(LON_KEY, pLong)
args.putDouble(LAT_KEY, pLat)
It looks like the problem is not with your code, but possibly with imports or your project setup.
I got this working in Android Studio by passing the the LatLng
Object as Parcelable in an Intent
between two Activities.
In my build.gradle:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:26.0.1'
compile 'com.google.android.gms:play-services-maps:12.0.0'
compile 'com.google.android.gms:play-services-location:12.0.0'
}
In both Activities, use the following import:
import com.google.android.gms.maps.model.LatLng;
Here is the test code that worked for me:
MainActivity.java:
double pLong = -121.345678;
double pLat = 37.123456;
LatLng fromPostion = new LatLng(pLat, pLong);
Bundle args = new Bundle();
args.putParcelable("longLat_dataPrivider", fromPostion);
Intent i = new Intent(this, MapActivity.class);
i.putExtras(args);
startActivity(i);
MapActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
Intent i = getIntent();
LatLng ll = i.getParcelableExtra("longLat_dataPrivider");
Log.d("Location", "location: " + ll.latitude + " " + ll.longitude);
}
LatLng
cannot be passed like that (sadly).
EDIT: Daniel Nugent exposed and proved that indeed LatLng
IS Parcelable. Therefor, it's solution is better than mine I must admit And I just learn something too.
I would suggest to save lat/lng values separately:
intent.putExtra("latitude", latLng.latitude);
intent.putExtra("longitude", latLng.longitude);
Then retrieve them like so:
final double latitude = getIntent().getDoubleExtra("latitude");
final double longitude = getIntent().getDoubleExtra("longitude");
final LatLng = new LatLng(latitude, longitude);