【发布时间】:2018-05-09 23:54:33
【问题描述】:
我需要使用我在 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));
}
}
【问题讨论】:
-
传递纬度经度...单独作为Double
标签: java android dictionary android-intent gps