使用ViewPager,Google Map不在Horizo​​ntalScrollView内部工作

时间:2015-01-15 13:10:12

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

我正在尝试将Google地图添加到ViewPager的Item之一,即Horizo​​ntalScrollView。我想在我的项目中显示商店的多个位置,因此使用Horizo​​ntalScrollView仅向googlemap滚动从左到右,在viewpager中完成页面。在viewpager中没有谷歌地图的每件事情都很好,但是当我添加谷歌地图来查看寻呼机时,我的应用程序崩溃了。

这是我的logcat和我到目前为止所做的努力。

My Pastebin

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

注意: ViewPager和谷歌地图都在工作,但一次不能同时工作。

我在3天内的搜索:123456,{{3} },789

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

请帮助我。

谢谢&问候 : Akki

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我通过在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);

现在一切正常。

答案 2 :(得分:0)

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的实例

in xml

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