【问题标题】:Google Map Not Working Inside HorizontalScrollView with ViewPager谷歌地图在使用 ViewPager 的 Horizo​​ntalScrollView 中不起作用
【发布时间】:2015-03-13 21:25:35
【问题描述】:

我正在尝试将谷歌地图添加到 ViewPager 的一项中,即 Horizo​​ntalScrollView。我想在我的项目中显示商店的多个位置,因此使用 Horizo​​ntalScrollView 从左到右滚动仅用于 googlemap 和 viewpager 内的完整页面。在 viewpager 中没有谷歌地图的情况下,一切都可以正常工作,但是当我添加谷歌地图来查看寻呼机时,我的应用程序崩溃了。

这是我的 logcat 和我迄今为止所做的努力。

My Pastebin

我很惊讶为什么这里是授权失败,但是在评论 viewpager 部分之后,它的工作就像魅力并显示正确的地图和其他东西。

注意: ViewPager 和 google map 都可以使用,但不能同时使用。

这3天我的搜索:1,2,3,4,5,6,7,8,10,@987654

上面给出的一些解决方案很棒,但我不知道为什么这些都不适用于我的情况。

请帮助我。

感谢和问候: 阿奇

【问题讨论】:

    标签: google-maps android-fragments android-viewpager mapfragment


    【解决方案1】:

    您可以在您的 api 演示中查看参考。放在目录中 日食:<installation-directory>/sdk/extras/google/google-play-services/samples/maps 你必须导入项目,然后你才能看到所有的东西。

    【讨论】:

    • 正如我在问题中已经提到的,google map 和 view pager 都是单独工作的,但是如何将 googlemap 同时放在 viewpager 中。
    【解决方案2】:

    我通过在 viewpager 中添加 mapview 来解决这个问题:

      LatLng StartPoint = new LatLng(latitude, longitude);
            LatLng EndPoint = new LatLng(28.474388, 77.503990);
    
            mapView.onCreate(bundle);
            mapView.onResume();  
            GoogleMap map = mapView.getMap();
    
            map.getUiSettings().setMyLocationButtonEnabled(false);
            map.setMyLocationEnabled(true);
    
            MapsInitializer.initialize(getActivity());
    
            // Updates the location and zoom of the MapView
            Marker startPoint = map.addMarker(new MarkerOptions().position(
                    StartPoint).title("startPoint"));
            Marker endPoint = map.addMarker(new MarkerOptions().position(EndPoint)
                    .title("endPoint"));
    
            // Move the camera instantly to EndPoint with a zoom of 15.
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(EndPoint, 10));
    
            // Zoom in, animating the camera.
            map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
    

    现在一切正常。

    【讨论】:

      【解决方案3】:
      import com.google.android.gms.maps.SupportMapFragment;
      
      SupportMapFragment mMapFragment = SupportMapFragment.newInstance();     
      FragmentManager fm = fragment.getChildFragmentManager();
      fm.beginTransaction().add(R.id.map, mMapFragment).commit();
      mMapFragment.getMapAsync(this);
      
      @Override
      public void onMapReady(GoogleMap googleMap) {
      mGoogleMap = googleMap;
      
      if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
          // TODO: Consider calling
          //    ActivityCompat#requestPermissions
          // here to request the missing permissions, and then overriding
          //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
          //                                          int[] grantResults)
          // to handle the case where the user grants the permission. See the documentation
          // for ActivityCompat#requestPermissions for more details.
          return;
      }
      mGoogleMap.setMyLocationEnabled(true);
      
      buildGoogleApiClient();
      
      mGoogleApiClient.connect();
      }
      
      protected synchronized void buildGoogleApiClient() {
      
      mGoogleApiClient = new GoogleApiClient.Builder(getContext())
              .addConnectionCallbacks(this)
              .addOnConnectionFailedListener(this)
              .addApi(LocationServices.API)
              .build();
      }
      
      @Override
      public void onConnected(Bundle bundle) {
      
      if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
          // TODO: Consider calling
          //    ActivityCompat#requestPermissions
          // here to request the missing permissions, and then overriding
          //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
          //                                          int[] grantResults)
          // to handle the case where the user grants the permission. See the documentation
          // for ActivityCompat#requestPermissions for more details.
          return;
      }
      Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
              mGoogleApiClient);
      if (mLastLocation != null) {
          //place marker at current position
          mGoogleMap.clear();
          //setLocation(mLastLocation.getLatitude(), mLastLocation.getLongitude());
      }
      
      
      mLocationRequest = new LocationRequest();
      mLocationRequest.setInterval(50000); //50 seconds
      mLocationRequest.setFastestInterval(30000); //30 seconds
      mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
      //mLocationRequest.setSmallestDisplacement(0.1F); //1/10 meter
      
       LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
      }
      
      @Override
      public void onConnectionSuspended(int i) {
      Toast.makeText(customAdapter.getContext(), "onConnectionSuspended", Toast.LENGTH_SHORT).show();
      }
      
      @Override
      public void onConnectionFailed(ConnectionResult connectionResult) {
      Toast.makeText(customAdapter.getContext(), "onConnectionFailed", Toast.LENGTH_SHORT).show();
      }
      
      @Override
      public void onLocationChanged(Location location) {
      sourceLat = location.getLatitude();
      sourceLng = location.getLongitude();
      getRestaurantDetails();
      latLng = new LatLng(location.getLatitude(), location.getLongitude());
      //latLng = new LatLng(lat, lng);
      //Log.wtf("Lat lng", lat + " " + lng);
      mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18));
      LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
      }
      

      fragment 是 viewPagerFragment 的实例

      在xml中

      <LinearLayout
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="250dp" />
      

      【讨论】:

        猜你喜欢
        • 2017-11-10
        • 1970-01-01
        • 2015-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多