gpt4 book ai didi

android - 通过 Intent 将经度和纬度传递给另一个类

转载 作者:行者123 更新时间:2023-11-29 15:12:06 25 4
gpt4 key购买 nike

我正在尝试将经度和纬度从 MainActivity 中的 onLocationChanged 传递到另一个包com.route.provider classDataPrivider 但我收到此错误怎么办我这样做?以及如何在 DataProvider 中接收它们?

        double pLong = location.getLongitude();
double pLat = location.getLatitude();

LatLng fromPostion = new LatLng(pLat,pLong );

Bundle args = new Bundle();
args.putParcelable("longLat_dataPrivider", fromPostion);

The method putParcelable(String, Parcelable) in the type Bundle is not applicable for the arguments (String, LatLng)

最佳答案

看起来问题不在于您的代码,而可能在于导入或您的项目设置。

我通过在两个 Activity 之间的 Intent 中将 LatLng 对象作为 Parcelable 传递,在 Android Studio 中实现了此功能。

在我的 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'
}

在这两个 Activity 中,使用以下导入:

import com.google.android.gms.maps.model.LatLng;

这是对我有用的测试代码:

主要 Activity .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);

map Activity .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);

}

关于android - 通过 Intent 将经度和纬度传递给另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30106507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com