高德地图显示在fragment中并添加按钮点击事件

2016/04/17//

///by xbw/

//环境  eclipse  api 22


先上一下效果图

话不多少,贴一下fragment的代码,高德地图的jar包导入就不细说了,官方很详细了,

ContactFragment.java

package com.example.matrix.fragment;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
import com.amap.api.maps.AMap;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
import com.example.Config.Config;
import com.example.matrix.GPSNaviActivity;
import com.example.matrix.R;
import com.example.matrix.menu.menu_1;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class ContactFragment extends Fragment implements LocationSource,
		AMapLocationListener {
	private AMap aMap;
	private MapView mapView;
	private OnLocationChangedListener mListener;
	private AMapLocationClient mlocationClient;
	private AMapLocationClientOption mLocationOption;
	private RadioGroup mGPSModeGroup;

	private TextView mLocationErrText;
	private View mapLayout;
	private ImageButton imgbtn;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (mapLayout == null) {
			mapLayout = inflater.inflate(R.layout.locationmodesource_activity,
					null);
			mapView = (MapView) mapLayout.findViewById(R.id.map);
			mapView.onCreate(savedInstanceState);
			if (aMap == null) {
				aMap = mapView.getMap();
				// init();
				setUpMap();
				mLocationErrText = (TextView) mapLayout
						.findViewById(R.id.location_errInfo_text);
				imgbtn = (ImageButton) mapLayout
						.findViewById(R.id.imageButton1);
			}
		} else {
			if (mapLayout.getParent() != null) {
				((ViewGroup) mapLayout.getParent()).removeView(mapLayout);
			}
		}
		return mapLayout;
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		init();
	}

	private void init() {
		mLocationErrText.setVisibility(View.GONE);
		imgbtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				Intent mIntent = new Intent();
				mIntent.setClass(getActivity(), GPSNaviActivity.class);
				getActivity().startActivity(mIntent);
			}
		});
		// imgbtn.setVisibility(View.GONE);
	}

	private void setUpMap() {
		aMap.setLocationSource(this);// 设置定位监听
		aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
		aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
		// 设置定位的类型为定位模式 ,可以由定位、跟随或地图根据面向方向旋转几种
		aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
	}

	@Override
	public void onLocationChanged(AMapLocation amapLocation) {
		// TODO 自动生成的方法存根
		Log.v("@@@@@@@@@@@@@@@", amapLocation.getLongitude() + "");
		Log.v("@@@@@@@@@@@@@@@", amapLocation.getLatitude() + "");
		Config.locs = amapLocation;
		if (mListener != null && amapLocation != null) {
			if (amapLocation != null && amapLocation.getErrorCode() == 0) {
				mLocationErrText.setVisibility(View.GONE);
				mListener.onLocationChanged(amapLocation);// 显示系统小蓝点
				imgbtn.setVisibility(View.VISIBLE);
			} else {
				String errText = "定位失败," + amapLocation.getErrorCode() + ": "
						+ amapLocation.getErrorInfo();
				Log.e("AmapErr", errText);
				mLocationErrText.setVisibility(View.VISIBLE);
				mLocationErrText.setText(errText);
			}
		}

	}

	@Override
	public void activate(OnLocationChangedListener listener) {
		// TODO 自动生成的方法存根
		mListener = listener;
		if (mlocationClient == null) {
			mlocationClient = new AMapLocationClient(getActivity());
			mLocationOption = new AMapLocationClientOption();
			// 设置定位监听
			mlocationClient.setLocationListener(this);
			// 设置为高精度定位模式
			mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
			// 设置定位参数
			mlocationClient.setLocationOption(mLocationOption);
			// 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
			// 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求
			// 在定位结束后,在合适的生命周期调用onDestroy()方法
			// 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
			mlocationClient.startLocation();
		}

	}

	@Override
	public void deactivate() {
		// TODO 自动生成的方法存根
		mListener = null;
		if (mlocationClient != null) {
			mlocationClient.stopLocation();
			mlocationClient.onDestroy();
		}
		mlocationClient = null;

	}

	/**
	 * 方法必须重写
	 */
	@Override
	public void onDestroy() {
		super.onDestroy();
		mapView.onDestroy();
		if (null != mlocationClient) {
			mlocationClient.onDestroy();
		}
	}

	/**
	 * 方法必须重写
	 */
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mapView.onSaveInstanceState(outState);
	}

	/**
	 * 方法必须重写
	 */
	@Override
	public void onPause() {
		super.onPause();
		mapView.onPause();
		deactivate();
	}

	/**
	 * 方法必须重写
	 */
	@Override
	public void onResume() {
		super.onResume();
		mapView.onResume();
	}

}

locationmodesource_activity.xml文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.amap.api.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </com.amap.api.maps.MapView>

    <TextView
        android:id="@+id/location_errInfo_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|left"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:background="@color/red"
        android:textColor="@color/darkgrey"
        android:text="TextView" 
        android:visibility="gone"/>
    
     <ImageButton
         android:id="@+id/imageButton1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:background="@drawable/img_3" />

</FrameLayout>


错误报告:The specified child already has a parent. You must call removeView() on the child's parent first.

if (mapLayout == null) {
			mapLayout = inflater.inflate(R.layout.locationmodesource_activity,
					null);
			mapView = (MapView) mapLayout.findViewById(R.id.map);
			mapView.onCreate(savedInstanceState);
			if (aMap == null) {
				aMap = mapView.getMap();
				// init();
				setUpMap();
				mLocationErrText = (TextView) mapLayout
						.findViewById(R.id.location_errInfo_text);
				imgbtn = (ImageButton) mapLayout
						.findViewById(R.id.imageButton1);
			}
		} else {
			if (mapLayout.getParent() != null) {
				((ViewGroup) mapLayout.getParent()).removeView(mapLayout);
			}
		}
		return mapLayout;

之前
mLocationErrText = (TextView) mapLayout
						.findViewById(R.id.location_errInfo_text);
				imgbtn = (ImageButton) mapLayout
						.findViewById(R.id.imageButton1);

这句放错了位置,导致按钮监听事件失效,






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值