2 xiaoluxiaohong xiaoluxiaohong 于 2014.08.26 11:11 提问

百度定位问题 高手看一下,这样写打不开

package com.example.baidumaphz;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.Overlay;
import com.example.baidumaphz.LocationApplication.MyLocationListener;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {
MapView mMapView = null;
BaiduMap mBaiduMap = null;
LocationClient mLocationClient = new LocationClient(getApplicationContext());
BDLocation location = null;

private String tempcoor = "gcj02";
public MyLocationListener mMyLocationListener = new MyLocationListener();
public TextView mLocationResult;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    SDKInitializer.initialize(getApplicationContext());
    setContentView(R.layout.activity_main);
    mMapView = (MapView) findViewById(R.id.bmapsView);
    mBaiduMap = mMapView.getMap();

    // 定位初始化
    mLocationClient = new LocationClient(this);
    LocationClientOption option = new LocationClientOption();
    option.setOpenGps(true);// 打开gps
    option.setCoorType("tempcoor");// 返回的定位结果是百度经纬度,默认值gcj02
    option.setScanSpan(1000);
    mLocationClient.setLocOption(option);
    mLocationClient.start();
    if (!mLocationClient.isStarted()) {
        mLocationClient.start();
        mLocationClient.requestLocation();
    } else {
        mLocationClient.requestLocation();
    }

    // 开启定位图层
    mBaiduMap.setMyLocationEnabled(true);
    // 构造定位数据
    MyLocationData locData = new MyLocationData.Builder()
            .accuracy(location.getRadius())
            // 此处设置开发者获取到的方向信息,顺时针0-360
            .direction(100).latitude(location.getLatitude())
            .longitude(location.getLongitude()).build();
    // 设置定位数据
    mBaiduMap.setMyLocationData(locData);
    // 设置定位图层的配置(定位模式,是否允许方向信息,用户自定义定位图标)
    BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory
            .fromResource(R.drawable.icon_st);
    MyLocationConfiguration config = new MyLocationConfiguration(null,
            true, mCurrentMarker);
    mBaiduMap.setMyLocationConfigeration(config);
    // 当不需要定位图层时关闭定位图层
    mBaiduMap.setMyLocationEnabled(false);
}

public class MyLocationListener implements BDLocationListener {

    @Override
    public void onReceiveLocation(BDLocation location) {
        // Receive Location
        StringBuffer sb = new StringBuffer(256);
        sb.append("time : ");
        sb.append(location.getTime());
        sb.append("\nerror code : ");
        sb.append(location.getLocType());
        sb.append("\nlatitude : ");
        sb.append(location.getLatitude());
        sb.append("\nlontitude : ");
        sb.append(location.getLongitude());
        sb.append("\nradius : ");
        sb.append(location.getRadius());
        if (location.getLocType() == BDLocation.TypeGpsLocation) {
            sb.append("\nspeed : ");
            sb.append(location.getSpeed());
            sb.append("\nsatellite : ");
            sb.append(location.getSatelliteNumber());
            sb.append("\ndirection : ");
            sb.append("\naddr : ");
            sb.append(location.getAddrStr());
            sb.append(location.getDirection());
        } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
            sb.append("\naddr : ");
            sb.append(location.getAddrStr());
            // 运营商信息
            sb.append("\noperationers : ");
            sb.append(location.getOperators());
        }
        logMsg(sb.toString());
        Log.i("BaiduLocationApiDem", sb.toString());
    }

    private void logMsg(String str) {
        // TODO Auto-generated method stub
        try {
            if (mLocationResult != null)
                mLocationResult.setText(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

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

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

@Override
protected void onDestroy() {
    // 退出时销毁定位
    if (mLocationClient != null)
        mLocationClient.stop();
    mMapView.onDestroy();
    super.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onRestoreInstanceState(savedInstanceState);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    return super.onCreateOptionsMenu(menu);
}

}

6个回答

sinat_24463043
sinat_24463043   2015.04.07 16:11

.xml呢?有没有增加权限

ljq49
ljq49   2015.06.24 16:08

xml文件是否加了如下服务申明
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >

ljq49
ljq49   2015.06.24 16:09

xml文件是否加了如下服务申明
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >

u013370255
u013370255   2015.08.19 16:04

是不是会崩溃啊?是不是location为null的原因啊?我把下面的代码放到onReceiveLocation(BDLocation location)里面就好啦
// 构造定位数据
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(100).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
// 设置定位数据
mBaiduMap.setMyLocationData(locData);
// 设置定位图层的配置(定位模式,是否允许方向信息,用户自定义定位图标)
BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory
.fromResource(R.drawable.icon_st);
MyLocationConfiguration config = new MyLocationConfiguration(null,
true, mCurrentMarker);
mBaiduMap.setMyLocationConfigeration(config);

    还有下面这句,我放在了onDestroy()里
    // 当不需要定位图层时关闭定位图层
mBaiduMap.setMyLocationEnabled(false);
baidu_27536529
baidu_27536529   2015.11.14 15:04

请问你解决了么?我现在也是这个问题

hbyclhy
hbyclhy   2016.04.13 17:31

LocationClient(getApplicationContext()); 这句话错了,应该是LocationClient(this);

Csdn user default icon
上传中...
上传图片
插入图片