按照别人博客的样例一步一步来写,别人给的demo能正常运行,但是我的就出现setAddrType("all")方法不能用,导包我都是直接复制demo的,应该不是导包的问题。这是demo的网址http://blog.csdn.net/hhhccckkk/article/details/39496495以下是我自己的代码package com.android.activity;
import android.os.Bundle;
import java.io.File;
import android.util.Log;
import java.math.BigDecimal;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.view.LayoutInflater;
import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import com.alibaba.fastjson.JSONObject;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.android.activity.MyOrientationListener.OnOrientationListener;
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.MapPoi;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfigeration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.BaiduMap.OnMapClickListener;
import com.baidu.mapapi.map.BaiduMap.OnMarkerClickListener;
import com.baidu.mapapi.map.MyLocationConfigeration.LocationMode;
import com.baidu.mapapi.model.LatLng;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
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.BaiduMap.OnMapClickListener;
import com.baidu.mapapi.map.BaiduMap.OnMarkerClickListener;
import com.baidu.mapapi.map.BaiduMapOptions;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow.OnInfoWindowClickListener;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
public class BaidudituActivity extends FinalActivity {
private LinearLayout mapLayout;
private BitmapDescriptor bitmap;
private InfoWindow mInfoWindow;
private LocationClient mLocClient;
public MyLocationListenner myListener = new MyLocationListenner();
private Button button1;
MyApplication myApp;
/**
* 地图控件
*/
private MapView mMapView = null;
/**
* 地图实例
*/
private BaiduMap mBaiduMap;
/**
* 定位的客户端
*/
private LocationClient mLocationClient;//= mLocClient
/**
* 定位的监听器
*/
boolean isFirstLoc = true;
/**
* 当前定位的模式
*/
private LocationMode mCurrentMode = LocationMode.NORMAL;
/***
* 是否是第一次定位
*/
private volatile boolean isFristLocation = true;
/**
* 最新一次的经纬度
*/
private double mCurrentLantitude;
private double mCurrentLongitude;
/**
* 当前的精度
*/
private float mCurrentAccracy;
/**
* 方向传感器的监听器
*/
private MyOrientationListener myOrientationListener;
/**
* 方向传感器X方向的值
*/
private int mXDirection;
/**
* 地图定位的模式
*/
private String[] mStyles = new String[] { "地图模式【正常】", "地图模式【跟随】",
"地图模式【罗盘】" };
private int mCurrentStyle = 0;
// 初始化全局 bitmap 信息,不用时及时 recycle
private BitmapDescriptor mIconMaker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
init();
mapLayout = (LinearLayout) findViewById(R.id.map);
BaiduMapOptions options = new BaiduMapOptions();
mBaiduMap.setOnMapClickListener(new OnMapClickListener() {
@Override
public boolean onMapPoiClick(MapPoi arg0) {
return false;
}
@Override
public void onMapClick(LatLng arg0) {
GeoCoder getCoder = GeoCoder.newInstance();
ReverseGeoCodeOption reCodeOption = new ReverseGeoCodeOption();
reCodeOption.location(arg0);
getCoder.reverseGeoCode(reCodeOption);
}
});
mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker arg0) {
return false;
}
});
}
public void init() {
BaiduMapOptions options = new BaiduMapOptions();
options.compassEnabled(false); // 不允许指南针
options.zoomControlsEnabled(false); // 不显示缩放按钮
options.scaleControlEnabled(false); // 不显示比例尺
mMapView = new MapView(this, options); // 创建一个map对象
mapLayout.addView(mMapView); // 把map添加到界面上
mBaiduMap = mMapView.getMap(); // 获取BaiduMap对象
mMapView.removeViewAt(1); // 去掉百度logo
mBaiduMap.setMyLocationEnabled(false); //不显示我的位置,样覆盖物代替
mBaiduMap.setMaxAndMinZoomLevel(18, 17);//图的最大最小缩放比例3-18
mLocClient = new LocationClient(this); //地图在tabhost中,请传入getApplicationContext()
mLocClient.registerLocationListener(myListener); //绑定定位监听
LocationClientOption option = new LocationClientOption(); //配置参数
option.setOpenGps(true);
/*这里出了问题*/option.setAddrType("all"); //设置使其可以获取具体的位置,把精度纬度换成具体地址
option.setCoorType("bd09ll");
option.setScanSpan(1000);
mLocClient.setLocOption(option);
mLocClient.start(); //开始定位
Intent intent = getIntent();
// 注意该方法要再setContentView方法之前实现
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.baiduditu_map_activity);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (myApp.getState().equals("0")) {
Intent intent = new Intent();
intent.setClass(BaidudituActivity.this, Page1Activity.class);
startActivity(intent);
} else if (myApp.getState().equals("1")) {
myApp.setState("2");
button1.setVisibility(View.GONE);
}
}
});
myApp = (MyApplication) getApplication();
if (myApp.getState().equals("0")) {
button1.setText("求 助");
} else if (myApp.getState().equals("1")) {
button1.setText("确定帮忙");
} else if (myApp.getState().equals("2")) {
button1.setVisibility(View.GONE);
}
mMapView = (MapView) findViewById(R.id.id_bmapView);
// 获得地图的实例
mBaiduMap = mMapView.getMap();
}
public class MyLocationListenner implements BDLocationListener { //定位
@Override
public void onReceiveLocation(BDLocation location) {
Log.d("hck", "定位定位");
if (location == null || mMapView == null)
return;
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius()).direction(100)
.latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mBaiduMap.setMyLocationData(locData);
if (isFirstLoc) {
Log.d("hck", "定位定位成功");
isFirstLoc = false;
LatLng ll = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
mBaiduMap.animateMapStatus(u);
mark(ll.latitude, ll.longitude, location.getAddrStr());
}
}
public void onReceivePoi(BDLocation poiLocation) {
}
}
private void setListener() {
mBaiduMap.setOnMapClickListener(new OnMapClickListener() { //点击地图事件
@Override
public boolean onMapPoiClick(MapPoi arg0) {
return false;
}
@Override
public void onMapClick(LatLng arg0) {
GeoCoder getCoder = GeoCoder.newInstance();
ReverseGeoCodeOption reCodeOption = new ReverseGeoCodeOption();
reCodeOption.location(arg0);
getCoder.reverseGeoCode(reCodeOption);
getCoder.setOnGetGeoCodeResultListener(new GetAddress());
}
});
mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() { //点击覆盖物事件
@Override
public boolean onMarkerClick(Marker arg0) {
showLocation(arg0);
return false;
}
});
}
private void showLocation(final Marker marker) { //显示气泡
// 创建InfoWindow展示的view
LatLng pt = null;
double latitude, longitude;
latitude = marker.getPosition().latitude;
longitude = marker.getPosition().longitude;
View view = LayoutInflater.from(this).inflate(R.layout.map_item, null); //自定义气泡形状
TextView textView = (TextView) view.findViewById(R.id.my_postion);
pt = new LatLng(latitude + 0.0004, longitude + 0.00005);
textView.setText(marker.getTitle());
// 定义用于显示该InfoWindow的坐标点
// 创建InfoWindow的点击事件监听者
OnInfoWindowClickListener listener = new OnInfoWindowClickListener() {
public void onInfoWindowClick() {
mBaiduMap.hideInfoWindow();//影藏气泡
}
};
// 创建InfoWindow
mInfoWindow = new InfoWindow(view, pt, listener);
mBaiduMap.showInfoWindow(mInfoWindow); //显示气泡
}
private void mark(double latitude, double longitude, String title) {//显示覆盖物
// 定义Maker坐标点
LatLng point = new LatLng(latitude, longitude);
// 构建Marker图标
bitmap = BitmapDescriptorFactory.fromResource(R.drawable.pointe_map);
// 构建MarkerOption,用于在地图上添加Marker
OverlayOptions option = new MarkerOptions().position(point)
.icon(bitmap);
// 在地图上添加Marker,并显示
Marker marker = (Marker) mBaiduMap.addOverlay(option);
marker.setTitle(title);
Bundle bundle = new Bundle();
bundle.putSerializable("recore", "ddd");
marker.setExtraInfo(bundle);
}
class GetAddress implements OnGetGeoCoderResultListener { //点击地图获取点击位置处的详细地址
@Override
public void onGetGeoCodeResult(GeoCodeResult arg0) {
}
@Override
public void onGetReverseGeoCodeResult(ReverseGeoCodeResult arg0) {
arg0.getAddress();
mBaiduMap.hideInfoWindow();
mark(arg0.getLocation().latitude, arg0.getLocation().longitude,
arg0.getAddress());
}
}
@Override
protected void onStop() {
// 关闭图层定位
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
// 在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
mMapView = null;
}
@Override
protected void onResume() {
super.onResume();
// 在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mMapView.onResume();
handler.post(runnable);
}
@Override
protected void onPause() {
super.onPause();
// 在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
mMapView.onPause();
}
private Runnable runnable = new Runnable() {
public void run() {
// 做操作
handler.sendEmptyMessage(1);
}
};
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
// 更新UI
init();
break;
}
};
};
}
添加回答
举报
0/150
提交
取消