当我使用Retrofit调用lon,lat来自服务器时,数据就在这里;
Double lon,lat;
NetworkEngine.getInstance().getResDetail(id, new Callback<Restaurant>() {
@Override
public void success(Restaurant restaurant, Response response) {
lon = restaurant.getLon(); // Double
lat = restaurant.getLat(); // Double
Log.i("lon_", String.valueOf(restaurant.getLon())); // lon_: -128.861048
Log.i("lat_", String.valueOf(restaurant.getLat())); // lat_: 51.09783
}
但@onMapReady lon,lat为null。为什么呢?
@Override
public void onMapReady(GoogleMap googleMap) {
Log.i("lonn_", String.valueOf(lon)); // null
Log.i("latt_", String.valueOf(lat)); // null
map = googleMap;
LatLng location = new LatLng(lat, lon);
}
答案 0 :(得分:1)
在您的Retrofit API调用之前调用onMapReady()
。
在您的Retrofit API调用的mapFragment.getMapAsync(this);
方法中初始化您的success()
。
答案 1 :(得分:1)
@onMapReady lon,lat为null。为什么呢?
因为lon和lat在这里没有值意味着两者都是null。我想你是在网络通话之前打开地图。你应该这样做 -
NetworkEngine.getInstance().getResDetail(id, new Callback<Restaurant>() {
@Override
public void success(Restaurant restaurant, Response response) {
lon = restaurant.getLon(); // Double
lat = restaurant.getLat(); // Double
Log.i("lon_", String.valueOf(restaurant.getLon())); // lon_: -128.861048
Log.i("lat_", String.valueOf(restaurant.getLat())); // lat_: 51.09783
openMap();// it's just a method name you can change it with user suitable method name.
}
}
在 openMap()方法中执行所有与地图相关的事情。
private void openMap(){
// do your stuff here..
// something like to call another method to open map for you or,
// write all the code here to open a map like this--
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
这只是一个想法,希望它会有所帮助。