谷歌地图缩放功能不起作用



目前我正在我的项目中研究谷歌地图。 我正在使用以下 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>

只需在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"/>

在代码中添加以下函数;

@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));      
}

相关内容

最新更新