【问题标题】:How to use a LatLng to a Google Maps Activity如何将 LatLng 用于 Google 地图活动
【发布时间】: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


【解决方案1】:

尝试像下面这样单独传递它

map.putExtra("latitude", latitude);
map.putExtra("longitude", longitude);

然后开始 MapsActivity 活动,如下所示

double lat = getIntent().getDoubleExtra("latitude");
double lang = getIntent().getDoubleExtra("longitude");

或将 LatLng 对象附加到 Bundle 的 putParcelable 方法

Bundle args = new Bundle();
args.putParcelable("Latlng", Local);
map.putExtra("bundle", args);

像进入 MapActivity 一样

Bundle bundle = getIntent().getParcelableExtra("bundle");
LatLng objLatLng = bundle.getParcelable("Latlng");

【讨论】:

  • getDoubleExtra 应该只接收一个参数吗?
  • @MatheusRibeiro 抱歉没有找到你
  • 这里 getDoubleExtra 需要一个 String 和一个 Double 值,例如 "getDoubleExtra(String, double);
  • @MatheusRibeiro 是的 getDoubleExtra 有 2 个值字符串作为键和双精度值作为默认值,例如 getDoubleExtra("doubleValue_e1", defaultValue)
  • 还是不行,应该是有问题,不是那个,还是谢谢
【解决方案2】:

这是因为

LatLng Local = new LatLng(latitude, longitude);

不可打包,您必须使用 Gson 将其转换为一些字符串,然后将其传递给 bundle 并再次使用 Gson 将其转换回对象

或者只使用 lat long 单独使用

putExtra("lat",latitude)
putExtra("long),longitude) 

【讨论】:

  • 即使使用metod,它仍然会停止
猜你喜欢
  • 2014-03-06
  • 2014-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多