最近因公司的一些特殊原因,接手一个安卓的项目,需求是将原有的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个项目,没有关联支付账号的项目是没有激活的!!!!!