当前位置:操作系统 > 安卓/Android >>

Android之百度地图 前期实现

Android之百度地图前期实现
//以下是写的几个类

package com.android.mapelves.org;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.ZoomControls;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.MyLocationOverlay;
import com.baidu.mapapi.OverlayItem;
@SuppressLint({ "ParserError", "ParserError" })
public class MainActivity extends MapActivity {
 private int lat;
 private int lon;
 //加载百度地图相关控件
 private MapView mapView;
 //加载地图引擎
 private BMapManager bMapManager;
 //百度地图key
 private String mapKey="64C2995A762A8C16C9A9DA8928B0F42489D5C106";
    //在百度地图上添加一些控件,放大与缩小
 private MapController mapController;
 // 地图覆盖物
    private MyLocationOverlay mLocationOverlay;
    //地图模式切换
   private ToggleButton modelChange;
   //设置是否开启实时路况
   private ToggleButton roadCond;
   //搜索
   private ImageView search;
   //线路
   private ImageView road;
   //附近
   private ImageView nearby;
   //导航
   private ImageView navBar;
 @SuppressLint("ParserError")
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        mapView = (MapView)this.findViewById(R.id.bmapView);
        modelChange = (ToggleButton)findViewById(R.id.model_change);
        roadCond = (ToggleButton)findViewById(R.id.road_cond);
        bMapManager = new BMapManager(MainActivity.this);
       
        Bundle bundle = new Bundle();
        bundle = this.getIntent().getExtras();
        lat = bundle.getInt("lat");
        lon = bundle.getInt("lon");
       
        //加载key
        bMapManager.init(mapKey, new MKGeneralListener() {
  
   public void onGetPermissionState(int arg0) {
    // TODO Auto-generated method stub
    if (arg0 == 300) {
     Toast.makeText(MainActivity.this, "验证失败", Toast.LENGTH_LONG).show();
    }
   }
  
   public void onGetNetworkState(int arg0) {
    // TODO Auto-generated method stub
   
   }
  });
       
        this.initMapActivity(bMapManager);
        mapView.setBuiltInZoomControls(true);//可以设置缩放功能
        mapView.setDrawOverlayWhenZooming(true);
        mapController = mapView.getController();
        //获取mapview中的缩放控件 
        ZoomControls zoomControls = (ZoomControls) mapView.getChildAt(2);
        //调整缩放控件的位置 
        zoomControls.setPadding(0, 0, 0, 100);
        mLocationOverlay = new MyLocationOverlay(this, mapView);
        mLocationOverlay.enableCompass();
        // 添加定位覆盖物
        mapView.getOverlays().add(mLocationOverlay);
        //事先定义各异经纬度
        mapController.setZoom(15);//缩放级别
        Drawable drawable = getResources().getDrawable(R.drawable.iconmarka);
        mapView.getOverlays().add(new MyOverlayItem(drawable));
        mapView.setTraffic(true);//显示交通地图
        mapView.invalidate();
        modelChange.setOnCheckedChangeListener(new ModelChangeListener());
        roadCond.setOnCheckedChangeListener(new RoadCondListener());
       
        nearby = (ImageView)findViewById(R.id.nearby);
        nearby.setOnClickListener(null);
       
        search = (ImageView)findViewById(R.id.search);
        search.setOnClickListener(new Searchlistener());
       
        road = (ImageView)findViewById(R.id.road);
        road.setOnClickListener(new Roadlistener());
       
        navBar = (ImageView)findViewById(R.id.nav);
        nearby.setOnClickListener(null);
       
 }
 //是否开启实时路况
 class RoadCondListener implements OnCheckedChangeListener{

  public void onCheckedChanged(CompoundButton buttonView,
    boolean isChecked) {
   // TODO Auto-generated method stub
   if (isChecked) {
    mapView.setTraffic(true);
    mapView.invalidate();
    Toast.makeText(MainActivity.this, "实时路况已开启", Toast.LENGTH_LONG).show();
   }else {
    mapView.setTraffic(false);
    mapView.invalidate();
    Toast.makeText(MainActivity.this, "实时路况已关闭", Toast

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,