地图不会放大

问题描述 投票:1回答:4

我正在使用带有登录系统的android studio创建一个谷歌地图应用程序。我的问题是地图不会放大。我只在地图上添加了一个简单的搜索栏。当我在没有登录系统的应用程序上运行相同的代码时,它可以很好地工作,但是,登录系统却没有。所以我的问题是,从下面的代码判断可能是什么原因?

我的app级build.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
    applicationId "com.mongodb.stitch.examples.mongorestaurant"
    minSdkVersion 19
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile files('libs/json-simple-1.1.1.jar')
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support.constraint:constraint-layout:1.0.1'
compile 'com.google.android.gms:play-services-maps:10.2.1'
compile 'com.google.android.gms:play-services-location:10.2.1'
compile 'com.google.android.gms:play-services-places:10.2.1'
compile 'org.mongodb:stitch:0.1.0-SNAPSHOT'
compile 'com.google.code.gson:gson:2.8.2'
testCompile 'junit:junit:4.12'
}


 repositories {

// TODO: Remove once BSON 3.5.0 is released
maven {
    url "https://oss.sonatype.org/content/repositories/snapshots"
}
}

我的MapsActivity.java

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
PlaceAutocompleteFragment placeAutoComplete;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    placeAutoComplete = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.place_autocomplete);
    placeAutoComplete.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {

            Log.d("Maps", "Place selected: " + place.getName());
            mMap.addMarker(new MarkerOptions().position(place.getLatLng())
                    .title("Marker in Sydney"));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(place.getLatLng()));
        }

        @Override
        public void onError(Status status) {
            Log.d("Maps", "An error occurred: " + status);
        }
    });


}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

}

}

我的activity_maps.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity"
android:orientation="vertical"
android:weightSum="1">

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

<fragment
    android:id="@+id/map"
    class="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</LinearLayout>

还有我的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mongodb.stitch.examples.mongorestaurant">

<!--
     The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
     Google Maps Android API v2, but you must specify either coarse or fine
     location permissions for the 'MyLocation' functionality. 
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <!--
         The API key for Google Maps-based APIs is defined as a string resource.
         (See the file "res/values/google_maps_api.xml").
         Note that the API key is linked to the encryption key used to sign the APK.
         You need a different API key for each encryption key, including the release key that is used to
         sign the APK for publishing.
         You can define the keys for the debug and release targets in src/debug/ and src/release/. 
    -->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

    <activity
        android:name=".MapsActivity"
        android:label="@string/title_activity_maps">
        <intent-filter>
            <action android:name="android.intent.action.MapsActivity" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

我将不胜感激任何帮助。提前致谢!

java android google-maps
4个回答
0
投票

你应该使用有助于自动缩放纬度和经度地址的相机焦点,使这个逻辑:

在我在MapsActivity类中添加此行,您可以在地图上添加标记:

CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(place.getLatLng(),12);
                    googleMap.moveCamera(cu);

这会对你有所帮助,它对我有所帮助。


0
投票

试试这个

private GoogleMap mMap;

private LatLngBounds AUSTRALIA = new LatLngBounds(new LatLng(-44, 113), new LatLng(-10, 154));
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(AUSTRALIA, 13));// 13 number is zoom parameter

0
投票

试试这个,在@Override onMapReady方法中添加以下行,

    mMap.getUiSettings().setZoomControlsEnabled(true);

0
投票

如果您需要在获取纬度和经度后放大地图,则必须更改此行。

mMap.moveCamera(CameraUpdateFactory.newLatLng(place.getLatLng()));

mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLng()), 13);

这里的13方法是缩放级别的地图,你可以根据你的应用需求改变这个缩放级别。

如果你想在谷歌地图上显示缩放控件

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.getUiSettings().setZoomControlsEnabled(true);
}
© www.soinside.com 2019 - 2024. All rights reserved.