setOnMyLocationChangeListener和setMyLocationEnabled在地图上不起作用

时间:2016-05-28 15:46:43

标签: android google-maps google-places-api google-places

我正在做这个链接所写的内容,但它不起作用(image1)(第46,47和53行)https://io2015codelabs.appspot.com/codelabs/fire-place#7

for image - > enter image description here MapsActiviy.java

import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.view.ViewTreeObserver;
import android.widget.Button;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private LatLngBounds.Builder mBounds = new LatLngBounds.Builder();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Set up Google Maps
        SupportMapFragment mapFragment = (SupportMapFragment)
                getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    /**
     * Map setup. This is called when the GoogleMap is available to manipulate.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        // Pad the map controls to make room for the button - note that the button may not have
        // been laid out yet.
        final Button button = (Button) findViewById(R.id.checkout_button);
        button.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        mMap.setPadding(0, button.getHeight(), 0, 0);
                    }
                }
        );
        mMap.setMyLocationEnabled(true);
        mMap.setOnMyLocationChangeListener(new OnMyLocationChangeListener() {
        @Override
            public void onMyLocationChange(Location location) {
               LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
               addPointToViewPort(ll);
               // we only want to grab the location once, to allow the user to pan and zoom freely.
               mMap.setOnMyLocationChangeListener(null);
            }
        });
    }

    private void addPointToViewPort(LatLng newPoint) {
        mBounds.include(newPoint);
        mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(mBounds.build(),
            findViewById(R.id.checkout_button).getHeight()));
    }
}

activity_maps.xml

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

    <Button android:id="@+id/checkout_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/check_out"
        android:onClick="checkOut"/>

    <fragment 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"
        android:id="@+id/map"
        tools:context=".MapsActivity"
        android:name="com.google.android.gms.maps.SupportMapFragment"/>

</FrameLayout>

build.gradle(模块:应用)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.3'
    defaultConfig {
        applicationId "com.sahikaaylin.checkout"
        minSdkVersion 19
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.google.android.gms:play-services:9.0.1'
    compile 'com.firebase:firebase-client-android:2.3.1'
    compile 'com.android.support:appcompat-v7:23.4.0'
}

for manifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >

        <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.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

1 个答案:

答案 0 :(得分:0)

如Android文档中所示:https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.OnMyLocationChangeListener

现在不推荐使用

GoogleMap.OnMyLocationChangeListener 。您正在使用的教程已经太旧了。

通过上面的链接,您将能够被重定向到更新的教程和操作指南(这也将解决 setMyLocationEnabled 错误)。

编辑:我推荐的内容现已弃用,例如软件开发的生命。如果您正在寻找最新的方式,请查看Android文档。