private List<LocaJSON> locaJSONS;
MapView mapView;
private GoogleMap mGoogleMap;
private static final String MAP_VIEW_BUNDLE_KEY = "MapViewBundleKey";
private int jlength;
private LatLng[] latLng;
2.onCreate中mapView = findViewById(R.id.mapView);
Bundle mapViewBundle = null;
if (savedInstanceState != null) {
mapViewBundle = savedInstanceState.getBundle(MAP_VIEW_BUNDLE_KEY);
}
mapView.onCreate(mapViewBundle);
mapView.getMapAsync(LocaDetail.this); //初始化,自動執行onMapReady3.若是要將繪圖實作在onclick中,記得要在資料取得後再行繪圖(初始化),否則會出錯。
4.自動複寫的方法(必須使用)
@Override
public void onMapReady(GoogleMap googleMap) { //相關功能設定 mGoogleMap=googleMap; mGoogleMap.setMinZoomPreference(12); mGoogleMap.setIndoorEnabled(true); UiSettings uiSettings = mGoogleMap.getUiSettings(); uiSettings.setIndoorLevelPickerEnabled(true); uiSettings.setMyLocationButtonEnabled(true); uiSettings.setMapToolbarEnabled(true); uiSettings.setCompassEnabled(true); uiSettings.setZoomControlsEnabled(true); drawMap(mGoogleMap); //以下實作繪圖的程式。 }
5.private void drawMap(GoogleMap mGoogleMap) { //參數必須為onMapReady中處理好的mGoogleMapdouble[][] jjss = new double[jlength][2]; Log.d("資料數 ", String.valueOf(jlength)); for(int i = 0 ; i < jlength ; i++) { LocaJSON jj = locaJSONS.get(i); double l_la = 0;double l_lo=0; try { NumberFormat ddf1= NumberFormat.getNumberInstance(); //將字串(locaJSONS的資料)轉為double型態 ddf1.setMaximumFractionDigits(5); l_la =ddf1.parse(jj.getLatitude()).doubleValue(); l_lo =ddf1.parse(jj.getLongitude()).doubleValue(); jjss[i][0]=l_la; jjss[i][1]=l_lo; } catch (ParseException e) { e.printStackTrace(); } } latLng = new LatLng[jlength]; for(int i=0;i<jlength;i++){ Log.d("LOCAddf ",i+" : "+jjss[i][0]+" : "+jjss[i][1]); latLng[i] = new LatLng(jjss[i][0], jjss[i][1]); //位置資訊打包 MarkerOptions markerOptions = new MarkerOptions().position(latLng[i]); mGoogleMap.addMarker(markerOptions); //加入繪製 } mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng[0],18)); //鏡頭以第一點為主 }
6.必須實作的生命週期相關方法
@Override
protected void onResume() { super.onResume(); mapView.onResume(); } @Override protected void onPause() { mapView.onPause(); super.onPause(); } @Override protected void onDestroy() { mapView.onDestroy(); super.onDestroy(); } @Override public void onLowMemory() { super.onLowMemory(); mapView.onLowMemory(); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState); }
7.
沒有留言:
張貼留言