2015-12-26 34 views
1

觸摸/揮筆我跟着this tutorial到地圖添加到我的標籤之一。它的工作原理,但是當我打開地圖選項卡上,我不能刷卡向上/向下,放大/縮小(除了當我雙擊)。滑動左/右作品,但地圖移動很少。我做了一個customViewPager,和禁止刷卡標籤,但並沒有爲他進行映射。地圖不檢測的Android

起初我用tabActivity和我的地圖碎片做過應該和這個樣子:

mapActivity extends FragmentActivity... 

由於tabActivity不贊成我加入的滑動選項卡,按照上面的教程。地圖無法FragmentActivity加入,只有片段,所以我改變地圖片段:

mapActivity extends Fragment... 

現在我有以下問題 - 我不能導航地圖。沒有檢測到滑動和觸摸輸入。我不能上下移動,放大或縮小,而我能做的唯一的事情是從左向右移動,但它是非常緩慢的。

回答

2

起初我加入tabLayout和viewPager到appBarLayout在app_bar_main(I由具有抽屜式導航的應用程序),和後來我移動appBarLayout到content_main和它的工作。

2

試試這個。其樣本。 100%的工作

public class Map extends Fragment { 

    final int RQS_GooglePlayServices = 1; 
    Location myLocation; 
    TextView tvLocInfo; 
    boolean markerClicked; 
    PolygonOptions polygonOptions; 
    Polygon polygon; 
    // GPSTracker class 
    GPSTracker gps; 
    double latitude; 
    double longitude; 
    MapView mapView; 
    GoogleMap map; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.activity_map_fragment, container, false); 
     // TODO Auto-generated method stub 
     MapsInitializer.initialize(getActivity()); 

     switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity())) { 
      case ConnectionResult.SUCCESS: 
       Toast.makeText(getActivity(), "SUCCESS", Toast.LENGTH_SHORT).show(); 
       mapView = (MapView) rootView.findViewById(R.id.map); 
       mapView.onCreate(savedInstanceState); 
       // Gets to GoogleMap from the MapView and does initialization stuff 
       if (mapView != null) { 
        map = mapView.getMap(); 
        map.getUiSettings().setMyLocationButtonEnabled(false); 
        map.setMyLocationEnabled(true); 
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 13); 
        map.animateCamera(cameraUpdate); 
       } 
       break; 
      case ConnectionResult.SERVICE_MISSING: 
       Toast.makeText(getActivity(), "SERVICE MISSING", Toast.LENGTH_SHORT).show(); 
       break; 
      case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED: 
       Toast.makeText(getActivity(), "UPDATE REQUIRED", Toast.LENGTH_SHORT).show(); 
       break; 
      default: 
       Toast.makeText(getActivity(), GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()), Toast.LENGTH_SHORT).show(); 
     } 

     gps = new GPSTracker(getActivity()); 

     if (gps.canGetLocation()) { 

      latitude = gps.getLatitude(); 
      longitude = gps.getLongitude(); 

      // \n is for new line 
      //  Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); 
     } else { 
      // can't get location 
      // GPS or Network is not enabled 
      // Ask user to enable GPS/network in settings 
      gps.showSettingsAlert(); 
     } 


     // tvLocInfo = (TextView) findViewById(R.id.locinfo); 

     /* FragmentManager myFragmentManager = getFragmentManager(); 
     MapFragment myMapFragment 
       = (MapFragment) myFragmentManager.findFragmentById(R.id.map); 
     map = myMapFragment.getMap();*/ 

     map.setMyLocationEnabled(true); 

     map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 


     markerClicked = false; 

     map.setMyLocationEnabled(true); 
     map.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() { 
      @Override 
      public void onMyLocationChange(Location location) { 

       CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude)); 
       CameraUpdate zoom = CameraUpdateFactory.zoomTo(11); 
       map.moveCamera(center); 
       map.animateCamera(zoom); 

      } 
     }); 


     /* LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     Criteria criteria = new Criteria(); 


     Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false)); 
     if (location != null) 
     { 
      map.animateCamera(CameraUpdateFactory.newLatLngZoom(
        new LatLng(location.getLatitude(), location.getLongitude()), 13)); 

      CameraPosition cameraPosition = new CameraPosition.Builder() 
        .target(new LatLng(location.getLatitude(), location.getLongitude()))  // Sets the center of the map to location user 
        .zoom(17)     // Sets the zoom 
        .bearing(90)    // Sets the orientation of the camera to east 
        .tilt(40)     // Sets the tilt of the camera to 30 degrees 
        .build();     // Creates a CameraPosition from the builder 
      map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

     }*/ 
     // Updates the location and zoom of the MapView 


     return rootView; 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onActivityCreated(savedInstanceState); 
    /* TextView tv = (TextView) getActivity().findViewById(R.id.textset); 
     tv.setText("Audio");*/ 
    } 

    @Override 
    public void onResume() { 
     mapView.onResume(); 
     super.onResume(); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     mapView.onDestroy(); 
    } 

    @Override 
    public void onLowMemory() { 
     super.onLowMemory(); 
     mapView.onLowMemory(); 
    } 
} 
+0

謝謝您的回覆聶。我試過了,但我得到了這個錯誤:無法解析符號'GPSTracker'。所以我評論它和我的應用程序。而現在我的地圖選項卡不顯示地圖,它只是一個藍屏。 – Kemo

+0

你的代碼有效,我只需要編輯XML文件,就像我在下面的答案一樣。 – Kemo