下面是源码,就四个java文件,请帮下忙[code=java]
package d.t;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.MKLocationManager;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.MyLocationOverlay;
import com.baidu.mapapi.Overlay;
import com.baidu.mapapi.Projection;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.TextView;
import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.TextView;
import android.widget.Toast;
public class Ditu extends MapActivity {
// 初始为于勉庄
private double userLongitude = 33.49087222349736 * 1E6;// 纬度
private double userLatitude = 115.27130064453128 * 1E6;// 经度
// 添加百度相关控件
private Location mLocation;
private LocationOverlay mLocationOverlay;
private MapView mapView;
private BMapManager bMapManager;// 加载地图的引擎
// 百度地图上的key值
private String keyString = "01331AFA954E7E300428A5F0C9C829E0E16F87A3";
// 在百度地图上添加一些控件,例如放大、缩小
private MapController mapController;
private MKLocationManager mLocationManager;
private int onceMyPoint = 1;
private static final int Search = Menu.FIRST;
private static final int SelectCity = Menu.FIRST + 1;
private static final int ViewMode = Menu.FIRST + 2;
private static final int Exit = Menu.FIRST + 3;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 实例化控件
mapView = (MapView) this.findViewById(R.id.bmapView);
bMapManager = new BMapManager(Ditu.this);
// 必须要加载key
bMapManager.init(keyString, new MKGeneralListener() {
// key值不正确
public void onGetPermissionState(int arg0) {
if (arg0 == 300) {
Toast.makeText(Ditu.this, R.string.key_error,
Toast.LENGTH_LONG).show();
}
}
//网络出错
public void onGetNetworkState(int arg0) {
Toast.makeText(Ditu.this, R.string.net_error,
Toast.LENGTH_LONG).show();
}
});
this.initMapActivity(bMapManager);
mapView.setBuiltInZoomControls(true);// 表示可以设置缩放功能
mapController = mapView.getController();
// 初始化Location模块
mLocationManager = bMapManager.getLocationManager();
if (onceMyPoint ==1) {
// 通过enableProvider和disableProvider方法,选择定位的Provider
mLocationManager.enableProvider(MKLocationManager.MK_NETWORK_PROVIDER);
mLocationManager.disableProvider(MKLocationManager.MK_GPS_PROVIDER);
//返回手机位置
mLocationManager.requestLocationUpdates(mLocationListener);
mLocationManager.setNotifyInternal(5, 2);
}
// 添加定位图层
MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this,
mapView);
myLocationOverlay.enableMyLocation(); // 启用定位
myLocationOverlay.enableCompass(); // 启用指南针
mapView.getOverlays().add(myLocationOverlay);
        mapController.setZoom(15);// 设置缩放级别
mapView.invalidate();// 刷新地图
}
// 定位自己的位置,只定位一次  
    private LocationListener mLocationListener = new LocationListener() {   
        public void onLocationChanged(Location location) {  
            if (location != null) {  
            userLatitude = location.getLatitude() * 1E6;  
                userLongitude = location.getLongitude() * 1E6;
                GeoPoint mypoint = new GeoPoint((int)(userLatitude), (int)(userLongitude)); 
                mLocationManager.removeUpdates(mLocationListener); 
                mapView.getController().animateTo(mypoint); 
                onceMyPoint++;  
            }  
        }  
    };
    /*
    static double DEF_PI = 3.14159265359; // PI 
    static double DEF_2PI= 6.28318530712; // 2*PI 
    static double DEF_PI180= 0.01745329252; // PI/180.0 
    static double DEF_R =6370693.5; // radius of earth
public double GetLongDistance(double lon1, double lat1, double lon2, double lat2) { 
double ew1, ns1, ew2, ns2; 
double distance; 
// 角度转换为弧度 
ew1 = lon1 * DEF_PI180; 
ns1 = lat1 * DEF_PI180; 
ew2 = lon2 * DEF_PI180; 
ns2 = lat2 * DEF_PI180; 
// 求大圆劣弧与球心所夹的角(弧度) 
distance = Math.sin(ns1) * Math.sin(ns2) + Math.cos(ns1) * Math.cos(ns2) * Math.cos(ew1 - ew2); 
// 调整到[-1..1]范围内,避免溢出 
if (distance > 1.0) 
distance = 1.0; 
else if (distance < -1.0) 
distance = -1.0; 
// 求大圆劣弧长度 
distance = DEF_R * Math.acos(distance); 
return distance;  } 
double mLat1 = 39.90923; // point1纬度 
double mLon1 = 116.357428; // point1经度 
double mLat2 = 39.90923;// point2纬度 
double mLon2 = 116.397428;// point2经度 
double adistance = GetLongDistance(mLon1, mLat1, mLon2, mLat2);MapViewTest mmapView=(MapViewTest)findViewById(R.id.bmapView);
class MapViewTest extends MapView{ 
private Object getProjection;
public MapViewTest(Context context) { 
super(context);  

public MapViewTest(Context context, AttributeSet attrs) { 
 super( context, attrs); 
 } 
public MapViewTest(Context context,AttributeSet attrs,int defStyle) { 
super( context, attrs,defStyle); 

public boolean onTouchEvent(MotionEvent event) { 
 //获得屏幕点击的位置 
int x = (int)event.getX(); 
int y = (int)event.getY(); 
//将像素坐标转为地址坐标 
 Projection projection = mapView.getProjection();
GeoPoint pt = ( (Projection)this.getProjection).fromPixels(x,y); 
return super.onTouchEvent(event);  

}
*/
//销毁
protected void onDestroy() {
super.onDestroy();
if (bMapManager != null) {
bMapManager.destroy();
bMapManager = null;
}
}
//Menu
//当点击Menu按钮时,调用该方法
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
menu.add(0, Search, Menu.NONE, "搜索地点").setIcon(R.drawable.search);
menu.add(0, SelectCity, Menu.NONE, "选择城市").setIcon(R.drawable.selectcity);
menu.add(0, ViewMode, Menu.NONE, "地图模式").setIcon(R.drawable.viewmode);
menu.add(0, Exit, Menu.NONE, "退出").setIcon(R.drawable.exit);
return true;
}
   
   public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch (item.getItemId())
{
case Search:
searchCity();
return true;
case SelectCity:
selectCity();
return true;
case ViewMode:
selectViewMode();
return true;
case Exit:
this.finish();
return true;
}
return true;
}
   
   //选择城市
   public void selectCity()
   {
    OnClickListener listener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which){
Double geoLat=ConstDate.cityCode[which][0]*1E6;
        Double geoLng=ConstDate.cityCode[which][1]*1E6;         mLocation.setLatitude(ConstDate.cityCode[which][0]);
        mLocation.setLongitude(ConstDate.cityCode[which][1]);
        mLocationOverlay.setLocation(mLocation);
        GeoPoint point=new GeoPoint(geoLat.intValue(),geoLng.intValue());
        //定位到指定坐标
        mapController.animateTo(point);
        
}
    };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ConstDate.city);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    new AlertDialog.Builder(Ditu.this)
       .setTitle("请选择城市")
       .setAdapter(adapter, listener)
       .show();
   }
   //选择地图模式
   public void selectViewMode()
   {
    OnClickListener listener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
switch ( which )
{

case 0:

mapView.setTraffic(true);
break;
case 1:

mapView.setSatellite(true);
break;
}
        
}
    };
    
    String[] menu={"交通流量","卫星地图"};
    new AlertDialog.Builder(Ditu.this)
       .setTitle("请选择地图模式")
       .setItems(menu, listener)
       .show();
   }
   //搜索城市
   public void searchCity()
   {
//自定义一个带输入的对话框由TextView和EditText构成
final LayoutInflater factory = LayoutInflater.from(Ditu.this);
final View dialogview = factory.inflate(R.layout.juli, null);
//设置TextView的提示信息
((TextView) dialogview.findViewById(R.id.TextView01)).setText("搜索地图");
//设置EditText输入框初始值
((EditText) dialogview.findViewById(R.id.EditText01)).setText("请输入要查找的地址...");

Builder builder = new Builder(Ditu.this);
builder.setTitle("请输入地名");
builder.setView(dialogview);
builder.setPositiveButton(android.R.string.ok,
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//点击确定之后
String value = ((EditText) dialogview.findViewById(R.id.EditText01)).getText().toString().trim();
if ( value != "" )
{
searchName(value);
}
}
});
builder.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
dialog.cancel();
}
});
builder.show();
   }
   
 

解决方案 »

  1.   

      public void searchName(String nameStr)
    {
    try
    {
    Geocoder mGeocoder = null;
    List<Address> addresses = mGeocoder.getFromLocationName(nameStr, 1);
    if (addresses.size() != 0)
    {
    Address address = addresses.get(0);
    GeoPoint geoPoint = new GeoPoint((int) (address.getLatitude() * 1E6), (int) (address.getLongitude() * 1E6)); mLocation.setLatitude(address.getLatitude());
    mLocation.setLongitude(address.getLongitude());
    mLocationOverlay.setLocation(mLocation); mapController.animateTo(geoPoint);
    }
    }
    catch (IOException e){}
    }// 停止
    protected void onPause() {
    super.onPause();
    if (bMapManager != null) {
    mLocationManager.removeUpdates(mLocationListener);
    bMapManager.stop();
    }
    }
    // 重启
    protected void onResume() {
    super.onResume();
    if (bMapManager != null) {
    bMapManager.start();
    }
    }
    //
    protected boolean isRouteDisplayed() {
    return false;
    }
    }
    [/code]
      

  2.   

    上面是第一个文件
    //文件二
    package d.t;import java.io.IOException;
    import java.util.List;
    import java.util.Locale;import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.graphics.Point;
    import android.location.Address;
    import android.location.Geocoder;
    import android.location.Location;import com.baidu.mapapi.GeoPoint;
    import com.baidu.mapapi.MapView;
    import com.baidu.mapapi.Overlay;public class LocationOverlay extends Overlay
    {
    private Location mLocation;
    private Ditu mMobileMap;
    private String mAddresses;
    public LocationOverlay(Ditu mobileMap)
    {
    mMobileMap = mobileMap;
    }
    public void setLocation(Location location)
    {
    mLocation = location;
    mAddresses = getAddresses();
    } public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
    {
    super.draw(canvas, mapView, shadow);
    Paint paint = new Paint();
    Point scPoint = new Point();
    GeoPoint tmpGeoPoint = new GeoPoint((int)(mLocation.getLatitude()*1E6),(int)(mLocation.getLongitude()*1E6));
    mapView.getProjection().toPixels(tmpGeoPoint, scPoint);

    paint.setStrokeWidth(1);
    paint.setARGB(255, 255, 0, 0);
    paint.setStyle(Paint.Style.STROKE);
    //消除锯齿
    paint.setFlags(Paint.ANTI_ALIAS_FLAG);
    paint.setTextSize(16);

    Bitmap bmp = BitmapFactory.decodeResource(mMobileMap.getResources(), R.drawable.);
    canvas.drawBitmap(bmp, scPoint.x-bmp.getWidth()/2, scPoint.y-bmp.getHeight(), paint);

    canvas.drawText(mAddresses, scPoint.x-paint.measureText(mAddresses)/2, scPoint.y, paint);
    return true;
    }

    public String getAddresses()
    {
    String addressString="没有找到地址";
    Geocoder gc=new Geocoder(mMobileMap,Locale.getDefault());
            try
            {
                List<Address> addresses=gc.getFromLocation(mLocation.getLatitude(), mLocation.getLongitude(),1);
                StringBuilder sb=new StringBuilder();
                if(addresses.size()>0)
                {
                    Address address = addresses.get(0);
                    sb.append("地址:");
    for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
    {
    sb.append(address.getAddressLine(i));
    }
    addressString=sb.toString();
                }
            }catch(IOException e){}
            
            return addressString;
    }
    }
      

  3.   

    //文件三package d.t;import android.content.Context;
    import android.util.AttributeSet;
    import android.view.MotionEvent;import com.baidu.mapapi.GeoPoint;
    import com.baidu.mapapi.MapView;
    import com.baidu.mapapi.Projection;
    class MapViewTest extends MapView{ 
    MapViewTest mmapView=(MapViewTest)findViewById(R.id.bmapView);
    private Object getProjection;
    public MapViewTest(Context context) { 
    super(context);  

    public MapViewTest(Context context, AttributeSet attrs) { 
     super( context, attrs); 
     } 
    public MapViewTest(Context context,AttributeSet attrs,int defStyle) { 
    super( context, attrs,defStyle); 

    public boolean onTouchEvent(MotionEvent event) { 
     //获得屏幕点击的位置 
    int x = (int)event.getX(); 
    int y = (int)event.getY(); 
    //将像素坐标转为地址坐标 
     Projection projection = mmapView.getProjection();
     
    GeoPoint pt =  ((Projection) this.getProjection).fromPixels(x,y); 
    return super.onTouchEvent(event);  


    }