Android : Google Map API 调用

最近因公司的一些特殊原因,接手一个安卓的项目,需求是将原有的app中调用百度地图的部分替换为谷歌地图(要将app拿到国外用),本着给钱的是老大的原则,硬着头皮上了只能!

前提
  • 在Google Cloud Platform申请到API KEY
效果图

在这里插入图片描述
将几个点连接起来,并设置起点与终点。

  • AndroidManifest.xml
<meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="AIzaSyB51fZbOkk5-TEDqEnL957MdEEcWjL35yf" />
  • build .gradle
    compile 'com.google.android.gms:play-services:11.0.4'
    compile 'com.google.maps.android:android-maps-utils:0.5+'
  • Layout布局XML文件
<FrameLayout
        android:id="@+id/fl_map"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.gms.maps.MapView
            android:id="@+id/bmapView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </FrameLayout>
  • Fragment
public class TrailFragment extends LGRSFragment implements OnMapReadyCallback{
     View mView;
     MapView mMapView ;
     GoogleMap mGoogleMap;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        return super.onCreateView(inflater, container, savedInstanceState);
        //加载界面内容
        if(mView == null){
            mView = x.view().inject(this, inflater, container);
            mMapView = (MapView) mView.findViewById(R.id.bmapView);
            mMapView.onCreate(savedInstanceState);
            mMapView.onResume();
            try{
                MapsInitializer.initialize(getActivity());
            }catch (Exception e) {
                e.printStackTrace();
            }
            mMapView.getMapAsync(this);
        }
        ViewGroup parent = (ViewGroup) mView.getParent();
        if (parent != null) {
            parent.removeView(mView);
        }
//        loadInfo();
        return mView;
    }
  public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;
        testDrawMap();
 }
public void testDrawMap(){
        LatLng startPoint = new LatLng(40.7200,-73.9900);
        LatLng endPoint = new LatLng(40.7110,-73.9920);
        mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom( startPoint, 15));
        Marker startMarker = mGoogleMap.addMarker(new MarkerOptions()
                .position(startPoint)
                .title("Start"));

        Marker endMarker = mGoogleMap.addMarker(new MarkerOptions()
                .position(endPoint)
                .title("End"));
        PolylineOptions rectOptions = new PolylineOptions()
                .add(startPoint)
                .add( new LatLng(40.7220,-73.9980))
                .add( new LatLng(40.7250,-73.98870))
                .add( new LatLng(40.7230,-73.9820))
                .add( new LatLng(40.7200,-73.9800))
                .add( new LatLng(40.7180,-73.9788))
                .add( new LatLng(40.7150,-73.9860))
                .add(endPoint);

        Polyline polyline = mGoogleMap.addPolyline(rectOptions.width(10)
                .color(Color.BLUE)
                .geodesic(true));
    }
问题

如果打开后只有Google的logo,其他的都加载不出,以此检查一下内容:

  • API KEY是否正确;
  • 发布版本与测试版本的SHA-1码是不一样的,检查是否与AK码上的配置一致;
    在这里插入图片描述
  • 检查网络
  • AK所在的项目是否关联了支付账号

最后这个问题,是我亲身经历的,反复检查都没发现配置与代码的问题,但一直加载不出地图,最后发现是我建了太多的测试项目,而一个支付账号默认只能关联3个项目,没有关联支付账号的项目是没有激活的!!!!!
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值