Pass longitude and Latitude with Intent to another class

后端 未结 3 779
情话喂你
情话喂你 2021-01-25 05:13

I am trying to pass the latitude and Longitude from the onLocationChanged in the MainActivity to another packagecom.route.provider classDataPrivider b

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-25 05:50

    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);
    
    }
    

提交回复
热议问题