我想在我的活动中放置一张地图,通过选择它来显示一个特定的位置纬度和经度我尝试这个代码但它不起作用任何人都可以告诉我为什么???
public class conactus extends Activity{
private GoogleMap map;
private LatLng myLoc;
double latitude=34.112924;
double longitude=35.650288;
@SuppressLint("NewApi") protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connectus);
MapFragment fm = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
fm.getMap();
Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
try {
List<Address> ad = geoCoder.getFromLocation(latitude, longitude,1);
if (ad.size() > 0) {
myLoc = new LatLng(
(int) (ad.get(0).getLatitude() * 1E6),
(int) (ad.get(0).getLongitude() * 1E6));
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(myLoc, 14);
map.animateCamera(update);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
首先尝试在真实设备上进行调试,有时候gmap不会在模拟器中显示。 还要检查map的调试密钥是否有效。 此外,如果仍有问题,请使用this指南,这很简单。
答案 1 :(得分:0)
您应该将fm.getMap();
更改为map = fm.getMap();