【问题标题】:google map zoom functionality not working谷歌地图缩放功能不起作用
【发布时间】:2017-08-04 09:59:52
【问题描述】:

目前我正在我的项目中使用 google-map。 我正在使用以下 API 版本和谷歌地图显示。

android.gms:play-services-maps:9.0.2
android.gms:play-services-location:9.0.2

问题:

当我尝试通过触摸缩放地图时,谷歌地图不会缩放并且 我也无法移动地图以查看其他位置。

这是我的代码:

@Override
public void onMapReady(GoogleMap googleMap) {

    //googleMap.getUiSettings().setZoomControlsEnabled(true);

    mMap = googleMap;

    Log.i(TAG, "onMapReady");


    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                  Toast.makeText(this,"Please enable GPS location on your phone",Toast.LENGTH_SHORT).show();

        return;
    }
    Log.i(TAG, "my location enabled");


    mMap.setMyLocationEnabled(true);
    map.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null);

    buildGoogleApiClient();
    if (mMap == null) {
        return;
    }
   /* currentLatLng= new LatLng (currentLocationLatitude,currentLocationLongitude);
    Log.i(TAG,"inside onMapready --->");*/

}

 protected synchronized void buildGoogleApiClient() {

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    mGoogleApiClient.connect();

}

*包含 XML 文件 * 这是我用来显示谷歌地图的 xml 布局文件。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"
xmlns:app="http://schemas.android.com/apk/res-auto">

<fragment android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mega.hertz.seeky.MainActivity"/>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="vertical"
    android:paddingBottom="@dimen/vertical_page_margin"
    android:paddingLeft="@dimen/horizontal_page_margin"
    android:paddingRight="@dimen/horizontal_page_margin"
    android:paddingTop="@dimen/margin_small">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="50dp">

    <fragment
        android:id="@+id/autocomplete_fragment"
        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</android.support.v7.widget.CardView>


</LinearLayout>
</ScrollView>

【问题讨论】:

  • 分享您的 XML 文件
  • 您好,我已包含 xml 文件

标签: android google-maps google-play-services


【解决方案1】:

只需在 ScrollView 底部添加您的片段。因为您的地图片段与 ScrollView 重叠。 改变你的xml:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</ScrollView>

<fragment android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mega.hertz.seeky.MainActivity"/>

【讨论】:

  • 当我在滚动视图后放置谷歌地图的片段并编译代码并检查 Ui 时,没有出现 placeautocimpletefragment 谷歌搜索地点,只有谷歌地图出现,但我可以缩放。我需要使用片段 plaveautofragment 进行谷歌地方搜索
  • 请分享您的“PlaceAutocompleteFragment”代码。你也可以参考这个链接:developers.google.com/places/android-api/autocomplete
  • 谢谢。我的代码是 PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.autocomplete_fragment); autocompleteFragment.setOnPlaceSelectedListener(this); ((EditText) autocompleteFragment.getView().findViewById(R.id.place_autocomplete_search_input)).setTextSize(15.0f);
  • 只需在 xml 文件中设置 ScrollView height = "wrap_content"。并在片段之后放置滚动视图。与您上传带有问题的 xml 文件相同。比你可以放大地图,也可以找到地方。
  • 谢谢。你的意思是地图片段后的滚动视图。应用它
【解决方案2】:

在您的代码中添加以下函数;

 @Override
public void onLocationChanged(Location location) {
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();

    LatLng latLng = new LatLng(latitude, longitude);
    mMap.addMarker(new MarkerOptions().position(latLng).title("My Location"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(20));      
}

【讨论】:

  • 此解决方案在加载地图时将地图移动到缩放级别。我的问题是,在加载谷歌地图后,当我缩放时它不起作用,而且我基本上也无法移动地图触摸地图不起作用
猜你喜欢
  • 2015-12-19
  • 1970-01-01
  • 1970-01-01
  • 2012-03-03
  • 2015-12-24
  • 1970-01-01
  • 2019-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多