如何使用该片段内的地图 [英] how to use maps inside the fragment
本文介绍了如何使用该片段内的地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以在里面部分平原引导我一定要标记添加到特定地理位置
<片段
机器人:ID =@ + ID /图
机器人:layout_width =match_parent
机器人:layout_height =100dp
机器人:名字=com.google.android.gms.maps.SupportMapFragment
/>
我不没有在Java code然而事情我伸出我的类片段
解决方案
不要忘记添加谷歌播放服务,如LIB库项目和
它应该共享项目的相同的工作空间。
块引用>Java的code
公共类SomeFragment扩展片段{ MapView类MapView类;
GoogleMap的地图; @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
视图V = inflater.inflate(R.layout.some_layout,集装箱,FALSE); //获取从XML布局的MapView和创建它
图形页面=(图形页面)v.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState); //从获取的MapView到的GoogleMap并执行初始化的东西
地图= mapView.getMap();
map.getUiSettings()setMyLocationButtonEnabled(假)。
map.setMyLocationEnabled(真); //需要做任何CameraUpdateFactory调用之前调用MapsInitializer
尝试{
MapsInitializer.initialize(this.getActivity());
}赶上(GooglePlayServicesNotAvailableException E){
e.printStackTrace();
} //更新的MapView的位置和缩放
的CameraUpdate的CameraUpdate = CameraUpdateFactory.newLatLngZoom(新经纬度(43.1,-87.9),10);
map.animateCamera(的CameraUpdate); 返回伏;
} @覆盖
公共无效onResume(){
mapView.onResume();
super.onResume();
} @覆盖
公共无效的onDestroy(){
super.onDestroy();
mapView.onDestroy();
} @覆盖
公共无效onLowMemory(){
super.onLowMemory();
mapView.onLowMemory();
}}布局
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT> < com.google.android.gms.maps.MapView机器人:ID =@ + ID /图形页面
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT/>< / LinearLayout中>清单
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com。示例
安卓版code =1
机器人:=的versionName1.0> <用途-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =15/> <使用许可权的android:NAME =android.permission.INTERNET对/>
<使用许可权的android:NAME =com.google.android.providers.gsf.permission.READ_GSERVICES/>
<使用许可权的android:NAME =android.permission.ACCESS_COARSE_LOCATION/>
<使用许可权的android:NAME =android.permission.ACCESS_FINE_LOCATION/> <用途特征
机器人:glEsVersion =0x00020000
机器人:要求=真/> <许可
机器人:名字=com.example.permission.MAPS_RECEIVE
安卓的ProtectionLevel =签名/>
<使用许可权的android:NAME =com.example.permission.MAPS_RECEIVE/> <应用
机器人:图标=@绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme> &所述;元数据
机器人:名字=com.google.android.maps.v2.API_KEY
机器人:值=your_key/> &所述;元数据
机器人:名字=com.google.android.gms.version
机器人:值=@整数/ GOOGLE_PLAY_SERVICES_VERSION/> <活动
机器人:HomeActivityNAME =
机器人:标签=@字符串/ APP_NAME>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.MAIN/>
<类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT; < /用途>< /清单>编辑:您的编辑新的问题出现了。
初始化地图检查,如果它不是后的空的距离的官方文档
如果(图!= NULL)
{
标记标记= map.addMarker(新的MarkerOptions()
.POSITION(新经纬度(37.7750,122.4183))
.title伪(旧金山)
是.snippet(人口:776733));
}i can show plain guide in inside part i have to add marker to specific geolocation
<fragment android:id="@+id/map" android:layout_width="match_parent" android:layout_height="100dp" android:name="com.google.android.gms.maps.SupportMapFragment" />
I don't did anything in java code however i extended my class with Fragment
解决方案Don't forget to add google-play-service-lib as a library project and it should sharing the same workspace of your project
Java code
public class SomeFragment extends Fragment { MapView mapView; GoogleMap map; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.some_layout, container, false); // Gets the MapView from the XML layout and creates it mapView = (MapView) v.findViewById(R.id.mapview); mapView.onCreate(savedInstanceState); // Gets to GoogleMap from the MapView and does initialization stuff map = mapView.getMap(); map.getUiSettings().setMyLocationButtonEnabled(false); map.setMyLocationEnabled(true); // Needs to call MapsInitializer before doing any CameraUpdateFactory calls try { MapsInitializer.initialize(this.getActivity()); } catch (GooglePlayServicesNotAvailableException e) { e.printStackTrace(); } // Updates the location and zoom of the MapView CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10); map.animateCamera(cameraUpdate); return v; } @Override public void onResume() { mapView.onResume(); super.onResume(); } @Override public void onDestroy() { super.onDestroy(); mapView.onDestroy(); } @Override public void onLowMemory() { super.onLowMemory(); mapView.onLowMemory(); } }
Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.google.android.gms.maps.MapView android:id="@+id/mapview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-feature android:glEsVersion="0x00020000" android:required="true"/> <permission android:name="com.example.permission.MAPS_RECEIVE" android:protectionLevel="signature"/> <uses-permission android:name="com.example.permission.MAPS_RECEIVE"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your_key"/> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <activity android:name=".HomeActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Edit: After your edit new question arise. After initializing map check if it is not null from
official documentation
if(map != null) { Marker marker = map.addMarker(new MarkerOptions() .position(new LatLng(37.7750, 122.4183)) .title("San Francisco") .snippet("Population: 776733")); }
这篇关于如何使用该片段内的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文