有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将GPS坐标传递给activity

我正在尝试将经度和纬度变量传递到Google地图片段的另一个活动中。到目前为止,我已经做到了这一点

Intent i = new Intent(Main.this, Main2.class);
i.putExtra(Double.toString(gettextLong), xLocation.getLongitude());
i.putExtra(Double.toString(gettextLat), xLocation.getLatitude());'

我如何在其他活动中接收它


共 (3) 个答案

  1. # 1 楼答案

    在第二个活动中使用getIntent()getDoubleExtra()。但要以不同的方式传递。putExtra中的第一个参数应该是某个字符串,但您必须了解他,需要在后面的第二个活动中使用。比如:

    public static final String FIRST = "firstArgument";
    
    i.putExtra(FIRST, xLocation.getLongitude()); 
    

    在Main2:

    double i = getIntent().getDoubleExtra(Main.FIRST, 0);
    
  2. # 2 楼答案

    试试看:

    Intent i = new Intent(Main.this, Main2.class);
    i.putExtra("lon", xLocation.getLongitude());
    i.putExtra("lat", xLocation.getLatitude());
    

    像这样:

    int lat = getIntent().getExtras().getDouble("lat");
    int lon = getIntent().getExtras().getDouble("lon");
    
  3. # 3 楼答案

    给出相关密钥,如

    Intent i = new Intent(Main.this, Main2.class);
    i.putExtra("longitude", xLocation.getLongitude());
    i.putExtra("latitude", xLocation.getLatitude());
    

    和Main2活动

    Bundle extras = getIntent().getExtras(); 
    if(extras !=null && extras.getDouble("latitude") != null && extras.getDouble("longitude") != null) { 
    double lat = extras.getDouble("latitude");
    double lng = extras.getDouble("longitude");
    }