我正在使用具有登录系统的android studio创建一个Google Maps应用程序。我的问题是地图无法放大。我仅向地图添加了一个简单的搜索栏。当我在没有登录系统的应用程序上运行相同的代码时,它可以完美运行,但是在登录系统上却不能。所以我的问题是,从下面的代码来看,可能是什么原因?

我的应用程序级别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>




我将不胜感激。提前致谢!

最佳答案

您应该使用相机对焦来帮助自动缩放您的纬度和经度地址,请执行以下逻辑:

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

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


这会帮助您,它会帮助我。

10-08 11:09