Android Map开发(MrMap源代码)

file:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mrsoft.mrmap.app.mr"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="3" />
    <uses-permission android:name="android.permission.RESTART_PACKAGES" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:icon="@drawable/maps" android:label="@string/app_name">
        <activity android:name=".main"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <uses-library android:name="com.google.android.maps"/>

    </application>
</manifest>

file:main.java

package com.mrsoft.mrmap.app.mr;

import java.util.List;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class main extends MapActivity {
	private MapView map;
	private TextView tv;
	private MapController mc;
	private LocationManager lm;
	private Criteria criteria;
	private Location location;
	private GeoPoint loacl;
	private List<Overlay> listOfOverlays;
	private MapOverlay mapOverlay;
	
	private final static int MENU_TRA = Menu.FIRST;
	private final static int MENU_SAT = Menu.FIRST+1;
	private final static int MENU_STR = Menu.FIRST+2;
	private final static int MENU_ABOUT = Menu.FIRST+3;
	private final static int MENU_EXIT = Menu.FIRST+4;
	private final static int MENU_GPS = Menu.FIRST+5;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        map = (MapView)findViewById(R.id.map);
        tv = (TextView)findViewById(R.id.tv);
        // 设置缩放
        map.setBuiltInZoomControls(true);
        initMap(39, 116);
        
        listOfOverlays = map.getOverlays();
        listOfOverlays.clear();
        
        
        lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        
        if (!lm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))
        {
        	Toast.makeText(this, "GPS已关闭,请手动开启GPS后再试!", Toast.LENGTH_SHORT).show();
        	return;
        }
        else
        {
        	Toast.makeText(this, "GPS定位中...", Toast.LENGTH_SHORT).show();
        }
        
        criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);	// 设置精确度
        criteria.setAltitudeRequired(true);				// 设置请求海拔
        criteria.setBearingRequired(true);				// 设置请求方位
        criteria.setCostAllowed(true);					// 设置允许运营商收费
        criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗
        
        String provider = lm.getBestProvider(criteria, true);
        location = lm.getLastKnownLocation(provider);
        newLocalGPS(location);
        // 监听1秒一次 忽略位置变化
        lm.requestLocationUpdates(provider, 1*1000, 0, new locationListener());
    }
    
    class locationListener implements LocationListener
    {

		@Override
		public void onLocationChanged(Location location) {
			// TODO Auto-generated method stub
			newLocalGPS(location);
		}

		@Override
		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
			newLocalGPS(null);
		}

		@Override
		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
			
		}
    	
    }
    
    public void newLocalGPS(Location location)
    {
    	if (location!=null)
    	{
    		double latitude = location.getLatitude(); //精度
    		double longitude = location.getLongitude(); // 纬度
    		double speed = location.getSpeed();	// 速度
    		double altitude = location.getAltitude();	// 海拔
    		tv.setText("精度"+latitude+'\n'+
    					   "纬度"+longitude+'\n'+
    					   "速度"+speed+"m/s"+'\n'+
    					   "海拔"+altitude+"m"+'\n');
    		initMap((int)latitude, (int)longitude);
    		MapOverlay mapOverlay = new MapOverlay(Color.GREEN, (int)latitude, (int)longitude);
    		listOfOverlays.add(mapOverlay);
    	}
    	else
    	{
    		// 未获取地理信息位置
    		tv.setBackgroundColor(Color.BLUE);
    		tv.setText("位置未知或正在获取中...");
    	}
    }
    
	public void initMap(int lat, int lon)
    {
    	// 设置初始地图的中心位置
        loacl = new GeoPoint(lat*1000000, lon*1000000);
        mc = map.getController();
        //mc.setCenter(loacl);
        mc.animateTo(loacl);
        mc.setZoom(10);
    }
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(0, MENU_TRA, 0, "交通视图");
		menu.add(0, MENU_SAT, 1, "街景视图");
		menu.add(0, MENU_STR, 2, "卫星视图");
		menu.add(1, MENU_GPS, 3, "定位");
		menu.add(1, MENU_ABOUT, 4, "关于");
		menu.add(1, MENU_EXIT, 5, "退出");
		return super.onCreateOptionsMenu(menu);
	}
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId())
		{
		case MENU_TRA:
			map.setTraffic(true);
			break;
		case MENU_SAT:
			map.setStreetView(true);
			break;
		case MENU_STR:
			map.setSatellite(true);
			break;
		case MENU_GPS:
			LayoutInflater inflater = LayoutInflater.from(this);//getLayoutInflater();
			View layout = inflater.inflate(R.layout.dg, null);
			final AlertDialog.Builder adb = new Builder(main.this); 
			adb.setTitle("定位");
			adb.setView(layout);
			adb.setPositiveButton("确定", new OnClickListener(){

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					AlertDialog ad = (AlertDialog) dialog;
					EditText j_et = (EditText)ad.findViewById(R.id.j_et);
					EditText w_et = (EditText)ad.findViewById(R.id.w_et);
					int lat = Integer.parseInt(j_et.getText().toString());
					int lon = Integer.parseInt(w_et.getText().toString());
					loacl = new GeoPoint(lat*1000000, lon*1000000);
					MapOverlay mapOverlay = new MapOverlay(Color.RED, lat, lon);
					listOfOverlays.add(mapOverlay);
					mc.animateTo(loacl);
					//initMap(Integer.parseInt(j_et.getText().toString()), Integer.parseInt(w_et.getText().toString()));
					dialog.dismiss();
				}
			});
			adb.setNegativeButton("取消", new OnClickListener(){

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					dialog.dismiss();
				}
			});
			adb.create().show();
			break;
		case MENU_ABOUT:
			 AlertDialog.Builder bd = new Builder(main.this);  
	            bd.setMessage("mrMap.apk\n版本:1.0\n作者:mrandexe");  
	            bd.setTitle("关于");  
	            bd.setPositiveButton("确认", new OnClickListener(){  
	                @Override  
	                public void onClick(DialogInterface arg0, int arg1) {  
	                    // TODO Auto-generated method stub  
	                    arg0.dismiss();  
	                }  
	            });  
	            bd.create().show(); 
			break;
		case MENU_EXIT:
			exit();
			break;
		}
		return super.onMenuItemSelected(featureId, item);
	}
    
    private void exit()  
    {  
    	 AlertDialog.Builder builder = new Builder(main.this);  
         builder.setMessage("确认退出吗?");  
         builder.setTitle("提示");  
         builder.setPositiveButton("确认", new OnClickListener(){  
             @Override  
             public void onClick(DialogInterface arg0, int arg1) {  
                 // TODO Auto-generated method stub  
                 arg0.dismiss();
                 ActivityManager actMgr = (ActivityManager)getSystemService(ACTIVITY_SERVICE);  
                 actMgr.restartPackage(getPackageName());
             }  
         });  
         builder.setNegativeButton("取消", new OnClickListener(){  

             @Override  
             public void onClick(DialogInterface dialog, int which) {  
                 // TODO Auto-generated method stub  
                 dialog.dismiss();  
             }  
         });  
         builder.create().show();   
    }
    
	@Override  
    public boolean onKeyDown(int keyCode, KeyEvent event) {  
        // TODO Auto-generated method stub  
        if (keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0)  
        {  
            exit();  
            return true;  
        }  
        return super.onKeyDown(keyCode, event);  
    }
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	// 标记
	class MapOverlay extends com.google.android.maps.Overlay
	{
		private int x = 0;
		private int y = 0;
		private int color;
		public MapOverlay(int color, int x, int y)
		{
			this.color = color;
			this.x = x;
			this.y = y;
		}
		@Override
		public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
				long when) {
			// TODO Auto-generated method stub
			Point screenPts = new Point();
			GeoPoint loacl = new GeoPoint(x*1000000, y*1000000);
            mapView.getProjection().toPixels(loacl, screenPts);
			Paint paint = new Paint();
			paint.setColor(color);
			canvas.drawCircle(screenPts.x, screenPts.y, 5, paint);
			return super.draw(canvas, mapView, shadow, when);
		}

	}
	
}

file:layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <com.google.android.maps.MapView
       android:id="@+id/map"
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"
       android:enabled="true"
       android:clickable="true"
       android:apiKey="076khi0v42LLy9gZ481gP9pAq9JXqOweGttgwHQ"
       />
    <TextView 
    android:layout_height="wrap_content" 
    android:id="@+id/tv" 
    android:layout_width="wrap_content"/>
</RelativeLayout>


file:layout/dg.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/dg" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content">
  <TextView android:id="@+id/textView1" android:text="经度" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></TextView>
  <EditText 
  android:text="" 
  android:layout_height="wrap_content" 
  android:layout_width="fill_parent" 
  android:layout_weight="1" 
  android:id="@+id/j_et"
  android:numeric="integer"/>
  <TextView android:id="@+id/textView2" android:text="纬度" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content"></TextView>
  <EditText 
  android:text="" 
  android:layout_height="wrap_content" 
  android:layout_width="fill_parent" 
  android:layout_weight="1" 
  android:id="@+id/w_et"
  android:numeric="integer"/>/>
</LinearLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值