1 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<!-- 使用第三方包,不使用本工程包 -->
<com.google.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0JJOMTfXnoZBpJ7zmZmlaKSHji-LbJbGz91ew"/>
</LinearLayout>
2 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cjf.map"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/><!-- 需要使用网络 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/><!-- GPS -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/><!-- NET -->"
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<uses-library android:name="com.google.android.maps"/><!-- 需要添加第三方库 -->
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
3, MainActivity
package com.cjf.map;
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.MyLocationOverlay;
import com.google.android.maps.Overlay;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
/**
*继承MapActivity抽象类,此类也是继承自Activity
*LocationManager需要监听地理位置信息的改变,由于多次需要用到此接口LocationListener,所以直接实现
**/
public class MainActivity extends MapActivity implements LocationListener{
private MapView mapView;
private MapController mMapController;
private GeoPoint mGeoPoint;
private LocationManager mLocationManager ;
private Location mLocation;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
/**
* 1,地图的加载
*/
mapView.setBuiltInZoomControls(true);//支持缩放
mapView.setClickable(true);//设置此方法可以和用户交互
mapView.setKeepScreenOn(true);
mapView.setLongClickable(true);
mapView.setStreetView(true);//设置为街景模式
/**
* 2,加入设置位置功能
*/
mMapController = mapView.getController();
mMapController.setZoom(18);//设置地图的放大级别
/**
* 3,给定经纬度,显示成都
*/
updateMapShow(30.659259,104.065765);
/**
* 4,在地图上给这个地点加上一个mark,标签(注意和地图不是同一层)
*/
mapView.getOverlays().add(new MyOverlay());
/**
* 5,实现定位功能
*/
mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//1,得到我们位置信息
mLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);//使用GPS,权限fine
if(mLocation != null){
//2,更新我们的位置信息
updateMapShow(mLocation.getLatitude(),mLocation.getLongitude());
}else
{
mLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);//使用网络,权限corase
if(mLocation != null)
updateMapShow(mLocation.getLatitude(),mLocation.getLongitude());
else
Toast.makeText(this, "得不到地理位置信息", 3).show();
}
/**
* 6,注册位置跟踪---网络追踪/GPS追踪
* 参数1:位置提供者
* 参数2:最小多长时间更新一次,时间都是毫秒单位
* 参数3:最小多长距离更新一次,单位米
* 参数4:注册监听事件LocationListener
*/
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000, 5, this);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
}
/**
* 根据坐标更新地理位置显示
* @param lat
* @param lng
*/
private void updateMapShow(Double lat,Double lng){
Double latI = lat*1E6;
Double lngI = lng*1E6;
mGeoPoint = new GeoPoint(latI.intValue(),lngI.intValue());
mMapController.animateTo(mGeoPoint);
mMapController.setCenter(mGeoPoint);//图片的左上角为中心点
//mMapController.setZoom(15);
}
/**
* 定义内部类,可以在地图上添加标签,这里在地图上添加一张图片标注当前位置
* @author Administrator
*
*/
class MyOverlay extends Overlay{
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
//取到一张图片
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Point out = null;
//返回值还是一个Point
out = mapView.getProjection().toPixels(mGeoPoint, out);//可以将地理坐标转换成屏幕的某个具体位置
Paint paint = new Paint();//这里不需要填充或设置
if(out != null)
canvas.drawBitmap(bitmap, out.x, out.y, paint);//添加一个标签
//返回值可以是true或者不改动
//return super.draw(canvas, mapView, shadow, when);
return true;
}
/**
* 点击的时候打印出地理位置的信息
*/
@Override
public boolean onTap(GeoPoint p, MapView mapView) {
Toast.makeText(MainActivity.this, "你点击了:"+p.getLatitudeE6()+" ; "+p.getLongitudeE6(), Toast.LENGTH_LONG).show();
return super.onTap(p, mapView);
}
}
/**
* 显示路线信息
*/
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
/**
* 添加三个菜单 ,选择地图的三种显示模式
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(1, 1, 1, "交通地图");
menu.add(1, 2, 3, "卫星地图");
menu.add(1, 3, 3, "街景地图");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case 1:
mapView.setTraffic(true);//因为三种模式是三个不同的图层,所以显示的时候一次只能显示一个图层
mapView.setSatellite(false);
mapView.setStreetView(false);
break;
case 2:
mapView.setTraffic(false);
mapView.setSatellite(true);
mapView.setStreetView(false);
break;
case 3:
mapView.setTraffic(false);
mapView.setSatellite(false);
mapView.setStreetView(true);
break;
}
return super.onOptionsItemSelected(item);
}
//-----------下面几个方法时LocationListener接口中的方法---------
/**
* 位置改变时触发
*/
public void onLocationChanged(Location location) {
if(location != null)
updateMapShow(location.getLatitude(),location.getLongitude());
Toast.makeText(MainActivity.this, "你当前的位置:"+location.getLatitude()+" ; "+location.getLongitude(), 3000).show();
}
public void onProviderDisabled(String provider) {
}
/**
* 位置提供者可用时触发
*/
public void onProviderEnabled(String provider) {
if(provider.equals(LocationManager.NETWORK_PROVIDER)){
Toast.makeText(MainActivity.this, "有移动网络可用", 3000);
}else if(provider.equals(LocationManager.GPS_PROVIDER)){
Toast.makeText(MainActivity.this, "有GPS可用", 3000);
}
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
//移除监听
@Override
protected void onPause() {
mLocationManager.removeUpdates(this);
super.onPause();
}
@Override
protected void onResume() {
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000, 5, this);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
super.onResume();
}
}