GoogleMap的不显示的变化我做 [英] GoogleMap not showing the changes I made
问题描述
我有,我用(或者说,想用)片段谷歌地图。
I have a fragment in which I use (or rather, want to use) Google Maps.
该片段是动作条和tabhost之间,片段的布局
The fragment is between an actionbar and a tabhost, the fragment's layout is
<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" />
和它看起来像这样
现在我想实际使用它。我跟着谷歌的地图文件/教程,并结束了与此code
Now I want to actually work with it. I followed Google's Map documentation/tutorial and ended up with this code
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 SDK 24.0.2和谷歌播放服务V22。我相应调整文件的gradle,这一切建立罚款。
Today I updated to the new Android 24.0.2 SDK and Google Play Services v22. I adjusted the gradle file accordingly and it all builds fine.
compile 'com.google.android.gms:play-services-maps:6.5.87'
compile 'com.google.android.gms:play-services-location:6.5.87'
有在code没有错误或警告和片段可一切都没有抱怨。
所需的权限和API密钥也被设置在清单中。
There are no errors or warnings in the code and the fragment loads everything without complaining. Required permissions and API key are also set in the manifest.
不过,我似乎无法真正与地图进行交互。在重写的方法都跑(我看到在日志中logcat的每个方法)
However, I seem unable to actually interact with the map. The overriden methods all run (I see the log for each method in logcat)
但尤其是这两条线似乎没有做任何事情。
but especially these 2 lines seem to not do anything.
googleMap.setMyLocationEnabled(true);
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
地图只是显示在上面的图片。当前位置没有显示(GPS,WiFi是启用),也不是改变了地图类型。尤其是地图键入不改变,我觉得奇怪,因为在我的日志,我可以看到它实际上做了改变(从1到4)。
The map just shows as in the picture above. The current location isn't shown (GPS, wifi is enabled), nor is the map type changed. Especially the map type not changing I find weird, because in my logs I can see it actually did change (from 1 to 4).
我在哪里去了?我缺少什么?
Where am I going wrong? What am I missing?
推荐答案
从@AbhinavPuri一些帮助,我得到它的工作。我所要做的就是
With some help from @AbhinavPuri I got it to work. What I had to do was
修改
<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" />
到
<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.SupportMapFragment" />
使我片段延长片段
而不是延长SupportMapFragment
和,因为我在一个片段,而不是使用的GoogleMap在活动中,我不得不把地图四处'code移动到onCreateView。如果你不这样做,findFragmentById返回null,因为在OnCreate,认为还没有膨胀。
Made my fragment extend Fragment
instead of extend SupportMapFragment
and, because I use a GoogleMap in a fragment and not in an activity, I had to move the 'map getting' code to the onCreateView. If you don't, findFragmentById returns null, because in the onCreate, the view is not yet inflated.
现在使用这块code的
Now using this piece of code
@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.
}
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return view;
}
使用getChildFragmentManager因为SupportMapFragment嵌套在另一个片段
Using getChildFragmentManager because the SupportMapFragment is nested in another fragment.
这篇关于GoogleMap的不显示的变化我做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!