android 开发百度地图


//首先创建一个application
package com.nxt;

import android.app.Application;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKEvent;
import com.baidu.mapapi.MKGeneralListener;

public class MapStart extends Application {

    BMapManager mBMapMan = null;

    static class MyGeneralListener implements MKGeneralListener {

        @Override
        public void onGetNetworkState(int iError) {

        }

        @Override
        public void onGetPermissionState(int iError) {
            if (iError == MKEvent.ERROR_PERMISSION_DENIED) {

            }
        }

    }

    @Override
    public void onCreate() {
        mBMapMan = new BMapManager(this);
        mBMapMan.init("C8AADF7B34DB122A953E714B7AA429EF9513B955",
                new MyGeneralListener());
    }

}
然后写
package com.nxt;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKLocationManager;
import com.baidu.mapapi.MKPoiInfo;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.MyLocationOverlay;
import com.baidu.mapapi.Overlay;
import com.baidu.mapapi.PoiOverlay;
import com.baidu.mapapi.Projection;

public class NearbyMap extends MapActivity implements MKSearchListener {
    public static final int ITEM0 = Menu.FIRST;
    public static final int ITEM1 = Menu.FIRST + 1;
    public static final int ITEM2 = Menu.FIRST + 2;
    public static NearbyMap byMap;
    public final static String SER_KEY = "com.nxt.mapview";
    public MapView mapView;// 地图
    private GeoPoint geopoint;// 得到地图上的坐标
    private int zoomlevel = 13;// 设置放大级别
    static Bitmap bitmap;// 气球图片
    static Bitmap farmerboolean;// 农家乐气球图片
    MyLocationOverlay myLocation;// 自己的当前位置
    LocationManager manager;// location管理类
    Drawable marker;// 任何资源
    List<Overlay> overlays;// 地图上的气球
    BMapManager mBMapMan = null;
    MKLocationManager locationManager = null;
    String mStrLog = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 得到location管理器

        setContentView(R.layout.nearbymap);
        // 从后面的activity传过来的地图放大缩小级别
        Intent intent = getIntent();
        marker = getResources().getDrawable(R.drawable.famerbollon); // 得到需要标在地图上的资源
        marker.setBounds(0, 0, marker.getIntrinsicWidth(),
                marker.getIntrinsicHeight()); // 为maker定义位置和边界
        byMap = this;
        mBMapMan = ((MapStart) getApplication()).mBMapMan;
        if (mBMapMan == null) {
            ((MapStart) getApplication()).mBMapMan = new BMapManager(
                    getApplication());
            // key申请网址:http://dev.baidu.com/wiki/static/imap/key/
            ((MapStart) getApplication()).mBMapMan.init("C8AADF7B34DB122A953E714B7AA429EF9513B955",
                    new MapStart.MyGeneralListener());
            mBMapMan = ((MapStart) getApplication()).mBMapMan;
        }
        // 如果使用地图SDK,请初始化地图Activity
        super.initMapActivity(mBMapMan);
        locationManager = mBMapMan.getLocationManager();
        locationManager
                .disableProvider((int) MKLocationManager.MK_GPS_PROVIDER);
        bitmap = BitmapFactory.decodeResource(this.getResources(),
                R.drawable.ballon);// 得到当前所在地的图片
        farmerboolean = BitmapFactory.decodeResource(this.getResources(),
                R.drawable.famerbollon);// 得到所有农家乐的图片
        mapView = getMapView();// mapView对象
        overlays = getOverLays();// Overlay对象
        // 从后面传过来的intent
        if (intent != null) {
            String target = intent.getStringExtra("level");// 地图的放大级别
            if (target != null) {
                zoomlevel = Integer.parseInt(intent.getStringExtra("level"));// 把放大级别设置成当前的放大级别

            }
        }
        
        // 如果intent不为空的话,把geopoint拿出来
        if (SurroundView.surroundView != null) {
            geopoint = SurroundView.surroundView.geopoint;
        }
        // 设置地图的缩放级别
        mapView.getController().setZoom(zoomlevel);
        // 搜索框
        final MKSearch mMKSearch = new MKSearch();
        mMKSearch.init(mBMapMan, this);
        ImageButton ib = (ImageButton) this.findViewById(R.id.ImageButtom01);// 获取搜索按钮
        ib.setOnClickListener(new OnClickListener() {
            // 重写onClick方法
            public void onClick(View v) {
                Intent intent = new Intent(NearbyMap.this, MlcxWebview.class);
                intent.putExtra(
                        "url",
                        "http://test.365960.com/item.php?act=search&searchsubmit=yes&mobile=ok&searchsort=subject");
                startActivity(intent);

            }
        });
        mapView.setBuiltInZoomControls(true); // 设置启用内置的缩放控件
        // 如果得到了当前的地址
        if (geopoint != null) {
            processLocationUpdated(geopoint);
            // 给移动屏幕添加监听器

        } else {

            geopoint = new GeoPoint(
            // 创建宝航地图中心经纬度的GeoPoint对象
                    (int) (39.9083 * 1E6),// 纬度
                    (int) (116.3975 * 1E6));

            mapView.getController().animateTo(geopoint);// 设置地图中心点经纬度
            GeoAndName geoandname[] = DataStore.initGeoAndName();// 得到农家乐景点的名称
            overlays.add(new FarmerBoolons(marker, this, geoandname)); // 把地图加到画布上
        }

        locationManager.requestLocationUpdates(mLocationListener01);
    }

    @Override
    protected void onDestroy() {
        if (mBMapMan != null) {
            mBMapMan.destroy();
            ((MapStart) getApplication()).mBMapMan = null;
            mBMapMan = null;
        }
        locationManager = null;

        super.onDestroy();
    }

    @Override
    protected void onPause() {
        if (mBMapMan != null) {
            mBMapMan.stop();
        }
        super.onPause();
    }

    @Override
    protected void onResume() {
        if (mBMapMan != null) {
            mBMapMan.start();
        }
        super.onResume();
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    // 当你移动的时候注册要触发的事件
    public final LocationListener mLocationListener01 = new LocationListener() {
        @Override
        public void onLocationChanged(Location location1) {
            // TODO Auto-generated method stub

            /* 当手机收到位置变更时,将location传入取得地理坐标 */
            processLocationUpdated(getGeoPointByLocation(location1));
        }
    };

    private void processLocationUpdated(GeoPoint point) {
        // 得到地图上的坐标
        geopoint = point;
        myLocation = new MyLocationOverlay(this, mapView);

        myLocation.enableMyLocation();
        myLocation.enableCompass(); // 打开指南针
        overlays.add(myLocation);
        mapView.getController().setCenter(point);
        drawNjlFarmerBoolon(point);// 画出农家乐地图

    }

    // 通过地理坐标找地图坐标
    private GeoPoint getGeoPointByLocation(Location location) {
        GeoPoint gp = null;
        try {
            // 手机上的坐标横着
            double width = location.getLatitude() * 1E6;
            // 手机上的坐标竖着
            double height = location.getLongitude() * 1E6;
            gp = new GeoPoint((int) width, (int) height);
        } catch (Exception e) {
        }
        // TODO Auto-generated method stub
        return gp;
    }

    // //得到windowManager对象
    public WindowManager getWinManager() {
        return (WindowManager) getSystemService(Context.WINDOW_SERVICE);
    }

    // 得到myLocationOverLay
    public MyLocationOverlay getMyLocationOverLay() {
        if (myLocation == null) {
            myLocation = new MyLocationOverlay(this, mapView);
        }
        return myLocation;
    }

    // 通过地理坐标点画出农家乐气球
    public void drawNjlFarmerBoolon(GeoPoint gp) {

        GeoPoint geos = mapView.getProjection().fromPixels(0, 0);// 把当前屏幕的左上角转换成经纬度
        // 当前屏幕的宽度
        int width = getWinManager().getDefaultDisplay().getWidth();
        // 当前屏幕的高度
        int height = getWinManager().getDefaultDisplay().getHeight();
        GeoPoint geos1 = mapView.getProjection().fromPixels(width, height);
        double x1 = geos.getLatitudeE6() / 1E6;// 得到屏幕左上角的经度
        double y1 = geos.getLongitudeE6() / 1E6;// 得到屏幕左上角的纬度
        double x2 = geos1.getLatitudeE6() / 1E6;// 得到屏幕右下角的经度
        double y2 = geos1.getLongitudeE6() / 1E6;// 得到屏幕右下角的纬度
        GeoAndName[] geoandnames = getGeoAndName(x1, x2, y1, y2, width, height,
                gp);// 交换一下小的经纬度在前

        FarmerBoolons njlBoolons = new FarmerBoolons(marker, this, geoandnames);// 创建farmerboolon对象
        overlays.add(njlBoolons);
    }

    // 将坐标换算成geopoint
    public GeoPoint getGeo(MapView mapView, int x, int y) {
        // 坐标翻译成经纬度
        Projection projection = mapView.getProjection();
        // 将屏幕坐标换算成Geopint
        return projection.fromPixels(x, y);
    }

    // x1,x2,y1,y2,按照小大,小大的顺序排列屏幕的宽,屏幕的高,
    public GeoAndName[] getGeoAndName(double x1, double x2, double y1,
            double y2, int width, int height, GeoPoint gp) {
        // 交换一下经纬度,大的在前,小的再后
        if (x1 > x2) {
            double temp = 0;
            temp = x2;
            x2 = x1;
            x1 = temp;

        }
        if (y1 > y2) {
            double temp = 0;
            temp = x2;
            x2 = x1;
            x1 = temp;
        }
        // DataStore.setGeoAndNames(x1,x2,y1,y2);
        double x = (x2 - x1) / height;// 一个像素代表的纬度
        double y = (y2 - y1) / width;// 一个像素代表的经度
        double lat1 = gp.getLatitudeE6() / 1E6 + height / 2 * x;// 屏幕左上角的坐标x
        double log1 = gp.getLongitudeE6() / 1E6 - width / 2 * y;// 屏幕左上角y
        double lat2 = gp.getLatitudeE6() / 1E6 - height / 2 * x;// 屏幕右下角x
        double log2 = gp.getLongitudeE6() / 1E6 + width / 2 * y;// 屏幕右下角y

//        DataStore.setGeoAndNames(lat2, lat1, log1, log2);// 设置当前的经纬度
        GeoAndName geoandname[] = DataStore.getGeoAndNames(lat2, lat1, log1, log2);// 得到geoandnames;
        return geoandname;
    }

    // 给menu菜单添加监听器
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        super.onCreateOptionsMenu(menu);
        menu.add(0, ITEM0, 0, "选择查询范围");
        menu.add(0, ITEM1, 0, "我的位置");
        menu.add(0, ITEM2, 0, "关于我们");
        menu.findItem(ITEM1);
        return true;

    }

    // 选择菜单项
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case ITEM0:
            surround();// 查找附近的公里数
            break;
        case ITEM1:
            if (geopoint != null) {
                processLocationUpdated(geopoint);
                // 给移动屏幕添加监听器

            } else {

                geopoint = new GeoPoint(
                // 创建宝航地图中心经纬度的GeoPoint对象
                        (int) (39.9083 * 1E6),// 纬度
                        (int) (116.3975 * 1E6));

                mapView.getController().animateTo(geopoint);// 设置地图中心点经纬度
            }
            break;
        case ITEM2:
            about();
            break;

        }
        return super.onOptionsItemSelected(item);
    }

    // 周围景点个数
    public void surround() {
        Intent intent = new Intent();
        intent.putExtra("lat", String.valueOf(geopoint.getLatitudeE6() / 1E6));// 纬度传过去
        intent.putExtra("lon", String.valueOf(geopoint.getLongitudeE6() / 1E6));// 经度传过去

        intent.setClass(NearbyMap.this, SurroundView.class);
        NearbyMap.this.startActivity(intent);// 把intent对象传过去
    }

    public void about() {
        Intent intent = new Intent(NearbyMap.this, MlcxWebview.class);
        intent.putExtra("url", "http://www.365960.com/about/about.html");
        startActivity(intent);
    }

    // 得到mapView
    public MapView getMapView() {
        if(mapView==null){
            mapView= (MapView) findViewById(R.id.bmapsView);
        }
        mapView.setOnClickListener(null);
        
        return mapView;
    }

    @Override
    public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onGetPoiResult(MKPoiResult result, int type, int isError) {
        if (result == null) {
            Toast.makeText(NearbyMap.this, "对不起,您输入的城市地图暂不支持",
                    Toast.LENGTH_SHORT).show();
            return;
        }
        MKPoiInfo first = result.getPoi(0);
        if (first != null) {
            PoiOverlay poioverlay = new PoiOverlay(NearbyMap.this, getMapView());
            geopoint = first.pt;
            poioverlay.setData(result.getAllPoi());

            getMapView().getOverlays().add(poioverlay);
            getMapView().getController().animateTo(geopoint);
        } else {
            Toast.makeText(NearbyMap.this, "对不起,您输入的城市地图暂不支持",
                    Toast.LENGTH_SHORT).show();
            return;
        }
    }

    // 得到Ovelays
    public List<Overlay> getOverLays() {

        return mapView.getOverlays();
    }

    @Override
    public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            main();
            return false;
        }
        return false;
    }

    // 返回到主程序
    public void main() {
        Intent intent = new Intent();
        intent.setClass(NearbyMap.this, MlcxMain.class);
        NearbyMap.this.startActivity(intent);// 把intent对象传过去
        NearbyMap.this.finish();
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值