android 内嵌地图,Android Fragment里嵌入高德地图【原创】
最近在做的項目里要用到地圖,看了一下高德地圖的API,最后決定就用高德地圖,和平時不同,這次地圖是要嵌在Fragment了,研究了一下網上的代碼,最后實現了。下面說一下實現2D地圖的方法。
1.先去高德地圖官網注冊Key,地址是http://lbs.amap.com/api/android-sdk/summary/;
2.根據說明下載所需的sdk.
3.配置工程
(1)添加key
在工程的“ AndroidManifest.xml ”文件如下代碼中添加Key.
(1)添加權限
4.布局文件
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
5.實現
public class FragmentMap extends Fragment{
private static FragmentMap fragment = null;
@ViewInject(R.id.map)
private MapView mapView;
private AMap aMap;
private View mapLayout;
public static Fragment newInstance() {
if (fragment == null) {
synchronized (FragmentMap.class) {
if (fragment == null) {
fragment = new FragmentMap();
}
}
}
return fragment;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (mapLayout == null) {
mapLayout = inflater.inflate(R.layout.fragment_map, null);
ViewUtils.inject(this, mapLayout);
mapView.onCreate(savedInstanceState);//必須寫
if (aMap == null) {
aMap = mapView.getMap();
} else {
if (mapLayout.getParent() != null) {
((ViewGroup) mapLayout.getParent()).removeView(mapLayout);
}
}
return mapLayout;
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
/**
* 方法必須重寫
* map的生命周期方法
*/
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
/**
* 方法必須重寫
* map的生命周期方法
*/
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
/**
* 方法必須重寫
* map的生命周期方法
*/
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
}
效果圖如下:
轉載時請注明出處及相應鏈接,本文永久地址:https://blog.yayuanzi.com/13773.html
微信打賞
支付寶打賞
感謝您對作者Lena的打賞,我們會更加努力!????如果您想成為作者,請點我
總結
以上是生活随笔為你收集整理的android 内嵌地图,Android Fragment里嵌入高德地图【原创】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android插件框架机制的选择,And
- 下一篇: android router不起作用,给