美文网首页Android
Android注册Google Map API

Android注册Google Map API

作者: 景山道人 | 来源:发表于2019-05-24 15:50 被阅读0次

    Reference:
    Android中Google Maps入门:基础知识
    实际上看上面这个就行了,我自己注册时发现界面有了点变化,故新写一篇,流程之类的是没差别的
    参考这个也行
    话说我不知道这俩需不需要梯子看诶,看不了的话就看我这个吧

    步骤:

    1. 去Google注册:
      Google Cloud控制台
    2. 创建一个新项目:


      左上角创建项目
    3. 填写项目名称:
      这里填写完后还会自动生成项目ID,这里的名称和ID好像无所谓,但我不确定,我自己测试的结果是跟这里的项目名称关系不大


      填写项目名称
    4. 创建成功后会进入你的项目里面,在搜索栏搜索你需要的API:


      看到这个巨大的Android SDK了嘛?
    5. 搜到之后点蓝色的“启用”,我这里已经完事了所以是“管理”


      蓝白的启用
    6. 启用完后进入控制台,点凭据,再点创建API KEY
      第三个
      创建时它会提示你“需不需要为你的API添加限制”,限制是肯定要加的,但是创建完了回头再加也可以,所以直接先拿到API KEY再说
      话说这个API KEY统一都是AIza开头的
    7. 注册完后之前那里的启用就变成管理了,点进来就是这里:


      密钥:你们看不见我
    8. 添加限制:点进去就行了,进入到这里


      这俩怎么填应该不用多说了吧
    9. 完事之后把库导下来:在build(app)里面,
    implementation 'com.google.android.gms:play-services-maps:16.1.0'
    implementation 'com.google.android.gms:play-services-location:16.0.0'
    

    然后在google_maps_api.xml里面,

    <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">YOUR_API_KEY</string>
    

    在Manifest里面,

    <meta-data
                android:name="com.google.android.geo.API_KEY"
                android:value="@string/google_maps_key" />
    <meta-data
                android:name="com.google.android.gms.version"
                android:value="@integer/google_play_services_version" />
    <!-- 这个记得写Application外面,上面的写在里面 -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    

    那个google_maps_key是得自己填的,那个文件没有的话创建上面那个google_maps_api.xml在values底下就好了,自己创建的话debug和release各一个
    google_play_services_version这个好像是会自己生成的,所以写在那就行,不用管了
    注意:要用这个Google Map需要Google Play Service

    使用

    这篇不是说这个的,所以随便写写,下面这个Activity是我直接新建地图项目自动生成的,所以看看就好
    如果要对地图接口进行设置,看到那个接口了嘛,这里实现了onMapReady方法,参数就是地图了,在这里对地图进行设置即可

    具体参考:
    Camer and View
    Developer官网

    package com.example.testmapapplication;
    
    import android.support.v4.app.FragmentActivity;
    import android.os.Bundle;
    import android.view.MotionEvent;
    
    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    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.MarkerOptions;
    
    public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    
        private GoogleMap mMap;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);
            // Obtain the SupportMapFragment and get notified when the map is ready to be used.
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            if (mapFragment != null) {
                mapFragment.getMapAsync(this);
            }
        }
    
        @Override
        public boolean dispatchTouchEvent(MotionEvent ev) {
            return super.dispatchTouchEvent(ev);
        }
    
        /**
         * Manipulates the map once available.
         * This callback is triggered when the map is ready to be used.
         * This is where we can add markers or lines, add listeners or move the camera. In this case,
         * we just add a marker near Sydney, Australia.
         * If Google Play services is not installed on the device, the user will be prompted to install
         * it inside the SupportMapFragment. This method will only be triggered once the user has
         * installed Google Play services and returned to the app.
         */
        @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;
            mMap.setMaxZoomPreference(11);
            mMap.setMinZoomPreference(10);
            mMap.setIndoorEnabled(false);
            mMap.setBuildingsEnabled(false);
            mMap.setTrafficEnabled(false);
            // 设置标记
            LatLng currPos = new LatLng(0, 0);
            mMap.addMarker(new MarkerOptions().position(currPos).title("Position of Ritchee"));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(currPos));
            // 把画面移动到标记点 
        }
    }
    

    关于Zoom的值,每个值代表精细到这么多:
    1: World
    5: Landmass/continent
    10: City
    15: Streets
    20: Buildings

    相关文章

      网友评论

        本文标题:Android注册Google Map API

        本文链接:https://www.haomeiwen.com/subject/tdwgzqtx.html