你好我正试图在我的Android应用程序中实现谷歌地图,我设法至少部分地这样做。我的地图中有很多错误(直到你在屏幕上点击几次才会显示,你不能放大,你不能只移动按屏幕,希望会发生一些事情) 这是我的代码:
public class LocatorFragment extends Fragment implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
GoogleMap.OnInfoWindowClickListener,
GoogleMap.OnMapLongClickListener,
GoogleMap.OnMapClickListener,
GoogleMap.OnMarkerClickListener{
private MapView mapView;
private GoogleMap map;
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.locator_fragment, parent, false);
mapView=(MapView)rootView.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
map = mapView.getMap();
// Defines the xml file for the fragment
return rootView;
}
// This event is triggered soon after onCreateView().
// Any view setup should occur here. E.g., view lookups and attaching view listeners.
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
}
@Override
public void onConnected(@Nullable Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onInfoWindowClick(Marker marker) {
}
@Override
public void onMapClick(LatLng latLng) {
}
@Override
public void onMapLongClick(LatLng latLng) {
}
@Override
public boolean onMarkerClick(Marker marker) {
return false;
}
}
这是我的布局:
<com.google.android.gms.maps.MapView
android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
因此,当我打开地图片段时,我得到一个空网格,然后在每次点击该网格时,它会加载一部分地图:哦,这真的很奇怪。我无法缩放或移动地图
答案 0 :(得分:0)
除了@ McAwesomeville的回答。
您需要仔细阅读更新的Google Maps Android API。 getMap
已被弃用。请改用getMapAsync(OnMapReadyCallback)
。回调方法为您提供了一个保证非空并准备好使用的GoogleMap实例。它会自动初始化地图系统和视图。
让您的活动实施git checkout branch-1_3 # switch to the 1.3 branch
git merge branch-1_2 # merge the 1.2 branch into 1.3
界面,然后在OnMapReadyCallback
回调中分配您的Google地图参考。
这是一个简单的例子:
onMapReady()