setOnMyLocationChangeListener 和 setMyLocationEnabled 在地图上不起作用
setOnMyLocationChangeListener and setMyLocationEnabled arent work on map
我正在执行此 link 中所写的操作,但它不起作用(图片 1)(第 46、47 和 53 行)https://io2015codelabs.appspot.com/codelabs/fire-place#7
图片 ->
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(模块:App)
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'
}
对于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>
如 Android 文档所示:https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.OnMyLocationChangeListener
GoogleMap.OnMyLocationChangeListener 现已弃用。您使用的教程太旧了。
从上面的 link,您将能够被重定向到更新的教程和操作指南(这也将解决 setMyLocationEnabled 错误)。
EDIT :我推荐的现在已弃用,这就是软件开发的生活。如果您正在寻找最新的方法,请查看 Android 文档。
我正在执行此 link 中所写的操作,但它不起作用(图片 1)(第 46、47 和 53 行)https://io2015codelabs.appspot.com/codelabs/fire-place#7
图片 ->
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(模块:App)
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'
}
对于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>
如 Android 文档所示:https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.OnMyLocationChangeListener
GoogleMap.OnMyLocationChangeListener 现已弃用。您使用的教程太旧了。
从上面的 link,您将能够被重定向到更新的教程和操作指南(这也将解决 setMyLocationEnabled 错误)。
EDIT :我推荐的现在已弃用,这就是软件开发的生活。如果您正在寻找最新的方法,请查看 Android 文档。