求解 Android使用百度map api写的地图,怎么定位自己位置并在进入界面显示?

a8237934 发布于 2012/03/31 14:47
阅读 9K+
收藏 0

使用的百度map api 但是定位每次都出现在天安门那里

代码 public void onLocationChanged(Location location) {

// TODO Auto-generated method stub

if (location != null) {

GeoPoint geoPoint = new GeoPoint(

(int) (location.getLatitude() * 1e6),

(int) (location.getLongitude() * 1e6));

mMapView.getController().animateTo(geoPoint);

mMapController = mMapView.getController();

求大神讲解,刚学Android什么都不会。都是看别人代码,看书慢慢写的,颇感不易啊、

 

加载中
0
otp
otp
模拟器里不能定位,在手机上运行就可以了
0
a8237934
a8237934

引用来自“otp”的答案

模拟器里不能定位,在手机上运行就可以了
谢谢 ~ 手机上能定位啦~ 
0
L
LeeStephen

引用来自“a8237934”的答案

引用来自“otp”的答案

模拟器里不能定位,在手机上运行就可以了
谢谢 ~ 手机上能定位啦~ 

你好,我是使用百度地图api的新手,但是写出来的程序也只能定位到天安门,

Location loc = new Location();
            this.latitude = loc.getValueOfLatitude() * 1e6;
            this.longtitude = loc.getValueOfLongtitde() * 1e6;
            
            GeoPoint point = new GeoPoint((int)latitude, (int) longtitude);
            myMapView.getController().animateTo(point);


具体代码示例如上,求大神告知···谢谢!

L
LeeStephen
回复 @a8237934 : 没有呢,我就是用真机测试还是出现这样的问题呢。。。
a8237934
a8237934
极有可能你用的模拟器
0
F
F-sky

哎,,,我也遇到这个问题了,你解决了没??

0
风过后

在manifest.xml中注册服务


<service
            android:name="com.baidu.location.f"
            android:enabled="true"
            android:permission="android.permission.BAIDU_LOCATION_SERVICE"
            android:process=":remote" >
        </service>


继承LocationListener这个类

添加接受类

class MyReceiveListenner implements BDLocationListener {
		
		public void onReceivePoi(BDLocation location) {
			ConstDefinition.latitude = location.getLatitude();
			ConstDefinition.longitude = location.getLongitude();
		}
		
		public void onReceiveLocation(BDLocation location) {
			ConstDefinition.latitude = location.getLatitude();
			ConstDefinition.longitude = location.getLongitude();
			
		}
	}



注册监听

private LocationClient mLocationClient = null; // 定位类
private MyReceiveListenner mListenner = new MyReceiveListenner();
mLocationClient = new LocationClient(getApplicationContext());
mLocationClient.registerLocationListener(mListenner);

开始定位

/**
	 * start定位
	 */
	public  void startLocation() {
		try {
			
				option = new LocationClientOption();
				option.setCoorType("bd09ll"); // 设置返回的坐标类型
				option.setPriority(LocationClientOption.GpsFirst); // 设置GPS优先  
				option.setScanSpan(myLocationTime); // 设置时间
				option.setAddrType("detail"); // 返回地址类型
				mLocationClient.setLocOption(option);
				mLocationClient.start(); // 打开定位
				if (mLocationClient != null && mLocationClient.isStarted()) 
					mLocationClient.requestLocation(); 
					
				
		} catch (Exception e) {
			e.printStackTrace();
		}
	}



附上我自己写的定位类供参考,若要使用继承此类,调用startLocation函数即可

package com.scics.activity.common;
import android.app.ProgressDialog;
import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.view.Window;
import android.widget.Toast;
import com.baidu.mapapi.BMapManager;
import com.scics.activity.R;
import com.scics.activity.common.ActNavigation.SearchListener;
import com.scics.common.ConstDefinition;
import com.scics.common.NetworkHandle;
import android.app.Activity;
import android.location.LocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.search.MKAddrInfo;
import com.baidu.mapapi.search.MKBusLineResult;
import com.baidu.mapapi.search.MKDrivingRouteResult;
import com.baidu.mapapi.search.MKPoiResult;
import com.baidu.mapapi.search.MKSearch;
import com.baidu.mapapi.search.MKSearchListener;
import com.baidu.mapapi.search.MKShareUrlResult;
import com.baidu.mapapi.search.MKSuggestionResult;
import com.baidu.mapapi.search.MKTransitRouteResult;
import com.baidu.mapapi.search.MKWalkingRouteResult;

public class BaiduActivity extends Activity implements LocationListener {

	
	private LocationClient mLocationClient = null; // 定位类
	private MyReceiveListenner mListenner = new MyReceiveListenner();
	private NetworkHandle netManager;  	//声明一个网络管理对象类的对象
	private static ProgressDialog _processBar;
	BMapManager mBMapMan = null;
	private LocationClientOption option = null;
	private int myLocationTime = 1 * 1000;
	protected MapView map_view;
	Context context;
	private static ProgressDialog processBar;
	
	
	
    @Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		//初始化百度地图API
		netManager = new NetworkHandle(getApplicationContext()); 	// creating connection detector class instance
		if(!netManager.isNetConnOpen()){
			Toast toast  = Toast.makeText(getApplicationContext(), "请打开网络连接,以便手机获取景区位置信息!", Toast.LENGTH_LONG);
			toast.show();
		}
		mBMapMan=new BMapManager(getApplication());  
		mBMapMan.init(ConstDefinition.BAIDU_KEY, null);   
		mLocationClient = new LocationClient(getApplicationContext());
		mLocationClient.registerLocationListener(mListenner);
		startLocation();
		_processBar = new ProgressDialog(this);
		_processBar.requestWindowFeature(Window.FEATURE_NO_TITLE);
		_processBar.setMessage(getResources().getString(R.string.tips_3));
		_processBar.show();
		_processBar.dismiss();
	}
    
    /**
	 * start定位
	 */
	public  void startLocation() {
		try {
			
				option = new LocationClientOption();
				option.setCoorType("bd09ll"); // 设置返回的坐标类型
				option.setPriority(LocationClientOption.GpsFirst); // 设置GPS优先  
				option.setScanSpan(myLocationTime); // 设置时间
				option.setAddrType("detail"); // 返回地址类型
				mLocationClient.setLocOption(option);
				mLocationClient.start(); // 打开定位
				if (mLocationClient != null && mLocationClient.isStarted()) 
					mLocationClient.requestLocation(); 
					
				
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 点击对话框周边可以隐藏对话框
	 * @param context
	 * 2013-11-11
	 */
	public final void  showProcessDialog(Context context){
		processBar = new ProgressDialog(context);
		processBar.requestWindowFeature(Window.FEATURE_NO_TITLE);
		processBar.setMessage(getResources().getString(R.string.tips_3));
		processBar.dismiss();
		if(!processBar.isShowing())
			processBar.show();
		else{
			processBar.dismiss();
			processBar.show();
		}
	}
	/**
	 * 点击周边不能隐藏对话框
	 * @param context
	 * 2013-11-11
	 */
	public final void showUnClickableProcessDialog(Context context){
		processBar = new ProgressDialog(context);
		processBar.requestWindowFeature(Window.FEATURE_NO_TITLE);
		processBar.setMessage(getResources().getString(R.string.tips_3));
		processBar.setCanceledOnTouchOutside(false); //设置不能几点界面周边
		processBar.dismiss();
		if(!processBar.isShowing())
			processBar.show();
		else{
			processBar.dismiss();
			processBar.show();
		}
	}
	public static void closeProcessDialog1(){
		if(null != processBar && processBar.isShowing())
			processBar.dismiss();
	}
	
	public void stopLocation(){
		mLocationClient.stop();
	}
	
class MyReceiveListenner implements BDLocationListener {
		
		public void onReceivePoi(BDLocation location) {
			ConstDefinition.latitude = location.getLatitude();
			ConstDefinition.longitude = location.getLongitude();
		}
		
		public void onReceiveLocation(BDLocation location) {
			ConstDefinition.latitude = location.getLatitude();
			ConstDefinition.longitude = location.getLongitude();
			
		}
	}
class SearchListener implements MKSearchListener {

	public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
//		city = arg0.addressComponents.city;
	}

	public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
	}

	public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {
	}

	public void onGetPoiDetailSearchResult(int arg0, int arg1) {
	}

	public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
	}

	public void onGetShareUrlResult(MKShareUrlResult arg0, int arg1, int arg2) {
	}

	public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
	}

	public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
	}

	public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
	}
	
}

	

//      百度地图重写方法
	@Override
	protected void onDestroy() {
		mLocationClient.stop();
		/*if (mBMapMan != null) {
			mBMapMan.destroy();
//			mBMapMan = null;
		}*/
		super.onDestroy();
	}
	

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	protected boolean isLocationDisplayed() {
		return false;
	}


	protected boolean isRouteDisplayed() {
		return false;
	}
	

	public static void showProcessDialog(){
		_processBar.show();
	}
	
	public static void closeProcessDialog(){
		if(_processBar!=null&&_processBar.isShowing())
			_processBar.dismiss();
	}
	
	
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

	public void onLocationChanged(Location location) {
		ConstDefinition.latitude = location.getLatitude();
		ConstDefinition.longitude = location.getLongitude();
		
	}
}




返回顶部
顶部