2013-07-22 46 views
1

我试图将经度和纬度变量传递给Google地图片段的另一个活动。到目前为止,我已设法做到这一点将GPS坐标传递给活动

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

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

回答

1

给相关的关键喜欢

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"); 
} 
+0

得到一些误差修改这里:类型不匹配无法从束转换为意图。方法getDouble(String)是未定义的类型意图 – ffs

+0

尝试更新的答案! –

+0

感谢您的帮助! – ffs

0

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

public static final String FIRST = "firstArgument"; 

i.putExtra(FIRST, xLocation.getLongitude()); 

而且在MAIN2:

double i = getIntent().getDoubleExtra(Main.FIRST, 0); 
1

尝试:

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