我需要使用在LocationChanged上获得的纬度和经度来在MAP上显示当时的位置,但是我不确定如何传递它!
我已经使用了Intent.putExtra,但是它使我的应用程序停顿了下来,但是我真的不知道为什么(当我不使用“ map.putExtra(” Latlng“,Local);”时,仍然会收到坐标)),但是添加了它破坏了我的应用程序,请帮助
//map is the name of the Intent of the google maps Activity
@Override
public void onLocationChanged(Location location) {
TextView coords=(TextView)findViewById(R.id.text_view_coords);
double latitude=location.getLatitude();
double longitude=location.getLongitude();
coords.setText("Latitude:"+Location.convert(latitude,Location.FORMAT_SECONDS)
+"\n" +
"Longitude:"+Location.convert(longitude,Location.FORMAT_SECONDS));
LatLng Local = new LatLng(latitude, longitude);
//Passar o Bundle referente ao LatLng criado anteriormente.
map.putExtra("Latlng", Local);
}
---------------------- MapsActivity ---------------------
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
double lat;
double lng;
LatLng objLatLng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
objLatLng=getIntent().getExtras().getParcelable("Latlng");
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//Recebendo do objeto LatLng as coordenadas em DOUBLE referentes a Latitude e a Longitude
lat = objLatLng.latitude;
lng = objLatLng.longitude;
//Referentes a marcação e setagem da posição atual na API do Google Maps
LatLng PosAtual = new LatLng(lat, lng);
mMap.addMarker(new MarkerOptions().position(PosAtual).title("Sua posição atual!"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(PosAtual));
}
}
最佳答案
原因是
LatLng Local = new LatLng(latitude, longitude);
是不可分割的,您必须使用
Gson
将其转换为某些String,然后将其传递给bundle并再次使用Gson将其转换回对象或仅通过使用
putExtra("lat",latitude)
putExtra("long),longitude)