我有一个片段,其中我使用(或者更确切地说,想要使用)Google 地图。
该片段位于操作栏和选项卡主机之间,该片段的布局是
<SeekBar
android:id="@+id/search_map_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="50"
android:progressDrawable="@drawable/seekbar_progress"
android:thumb="@drawable/seekbar_thumb"
android:layout_marginTop="5dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/seekbar_min"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/seekbar_mid"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/seekbar_max"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"/>
</RelativeLayout>
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment" />
看起来像这样
现在我想实际使用它。我遵循谷歌地图文档/教程并最终得到了这段代码
public class SearchFriendsMapFragment extends SupportMapFragment implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private static View view;
private GoogleMap map;
private GoogleApiClient googleApiClient;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
buildGoogleApiClient(activity);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getMapAsync(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
/* If you place a mapfragment inside a fragment, it crashes when the fragment is
* loaded a 2nd time. Below solution was found at http://stackoverflow.com/questions/
* 14083950/duplicate-id-tag-null-or-parent-id-with-another-fragment-for-com-google-androi
*/
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null) {
parent.removeView(view);
}
}
try {
// Inflate the layout for this fragment.
view = inflater.inflate(R.layout.fragment_search_friends_map, container, false);
} catch (InflateException e) {
// Map is already there, just return view as it is.
}
return view;
}
@Override
public void onStart() {
super.onStart();
googleApiClient.connect();
}
@Override
public void onStop() {
googleApiClient.disconnect();
super.onStop();
}
protected synchronized void buildGoogleApiClient(Context context) {
Log.i(LogUtil.TAG, "BUILDING GOOGLE API CLIENT");
googleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onMapReady(GoogleMap googleMap) {
Log.i(LogUtil.TAG, "MAP READY");
Log.i(LogUtil.TAG, String.valueOf(googleMap.getMapType()));
googleMap.setMyLocationEnabled(true);
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
Log.i(LogUtil.TAG, String.valueOf(googleMap.getMapType()));
//map = googleMap;
}
@Override
public void onConnected(Bundle bundle) {
Log.i(LogUtil.TAG, "CONNECTED");
Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if (lastLocation != null) {
Log.i(LogUtil.TAG, String.valueOf(lastLocation.getLatitude()));
Log.i(LogUtil.TAG, String.valueOf(lastLocation.getLongitude()));
LatLng latlng = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
//map.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng, 5));
}
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.e(LogUtil.TAG, "CONNECTION FAILED");
}
@Override
public void onConnectionSuspended(int i) {
Log.i(LogUtil.TAG, "CONNECTION SUSPENDED");
}
}
今天我更新到了新的 Android 24.0.2 SDK 和 Google Play Services v22。我相应地调整了 gradle 文件,一切都构建得很好。
compile 'com.google.android.gms:play-services-maps:6.5.87'
compile 'com.google.android.gms:play-services-location:6.5.87'
代码中没有错误或警告,并且片段加载所有内容而不会抱怨。
所需的权限和 API 密钥也在清单中设置。
但是,我似乎无法实际与地图交互。重写的方法全部运行(我在 logcat 中看到每个方法的日志)
但尤其是这两行似乎没有做任何事情。
googleMap.setMyLocationEnabled(true);
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
地图就如上图所示。未显示当前位置(GPS、WiFi 已启用),地图类型也未更改。特别是地图类型没有改变,我觉得很奇怪,因为在我的日志中我可以看到它实际上确实发生了变化(从 1 到 4)。
我哪里错了?我缺少什么?