如何使用Google Map API加载地图?

时间:2015-03-18 18:35:16

标签: java android google-maps google-api

加载MapFragment时出现问题。 在手机的屏幕上,它什么都不显示。

公共类MapsActivity扩展了FragmentActivity {

private GoogleMap mMap; // Might be null if Google Play services APK is not available.
protected ArrayList<GPSPoint> coord = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    //setUpMapIfNeeded();
    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
            .getMap();
    LatLng HAMBURG = new LatLng(53.558, 9.927);
    Marker hamburg = mMap.addMarker(new MarkerOptions().position(HAMBURG)
            .title("Hamburg"));
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));


}

2 个答案:

答案 0 :(得分:0)

mapFragment =(MapFragment)getFragmentManager()                         .findFragmentById(R.id.map);
                mapFragment.getMapAsync(本);
这对我有用

答案 1 :(得分:0)

尝试类似的东西:

 public class MainActivity extends FragmentActivity {

 private final LatLng STARTING_POINT=new LatLng(45.464711, 9.188736);

 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   GoogleMap map=((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
   map.moveCamera(CameraUpdateFactory.newLatLngZoom(STARTING_POINT, 5));
 }
}

缩放地图:

map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);

添加图钉:

// Default pin
map.addMarker(new MarkerOptions().position(STARTING_POINT));

// Custom pin
map.addMarker(new MarkerOptions().position(STARTING_POINT).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));

P.S。首先,您需要获取API密钥,在Google Developer Console中启用Google Maps API并修改您的应用的清单,设置相应的权限。 Yuo做到了,对吧?