Google Map SDK获取经纬度

官方文档

GitHub Demo2

GitHub Demo2

上述两个Demo都是googlemaps推出的,但是Demo1中关于map的布局与Demo2中稍有不同,导致Demo1中获取map对象失败。

Demo1 布局
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />
Demo2 布局
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="0dp"
    android:layout_height="0dp" />

创建API密钥

控制台地址

1546417278839

1546417332239

1546417408590

配置API密钥

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<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">
    <!--Google Map API密钥配置-->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="AIzaSyBIyDgkIn_yaFB2qqnHZvRWi6W8Vr08wsk" />
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Google Map XML

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="0dp"
    android:layout_height="0dp" />

代码

findViewById(R.id.loaction).setOnClickListener(v -> {
    new RxPermissions(this)
            .requestEach(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)//请求权限
            .subscribe(permission -> {
                if (permission.granted) {//同意
                    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
                    mapFragment.getMapAsync(new OnMapReadyCallback() {
                        @SuppressLint("MissingPermission")
                        @Override
                        public void onMapReady(GoogleMap agoogleMap) {
                            if (agoogleMap == null) {
                                Toast.makeText(MainActivity.this, "没有获取到map对象", Toast.LENGTH_LONG).show();
                                return;
                            }
                            //可以获取定位
                            agoogleMap.setMyLocationEnabled(true);
                            agoogleMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
                                @Override
                                public void onMyLocationChange(Location location) {
                                    Toast.makeText(MainActivity.this, "Current location:\n" + location, Toast.LENGTH_LONG).show();
                                    Log.e("地址", location.toString());
                                    //获取位置之后取消定位
                                    agoogleMap.setMyLocationEnabled(false);
                                }
                            });
                        }
                    });
                } else {
                    Toast.makeText(MainActivity.this, "请打开定位权限", Toast.LENGTH_SHORT).show();
                }
            });
});

猜你喜欢

转载自blog.csdn.net/MoLiao2046/article/details/85623475