Android开发要连接GoogelMaps使用MapView时需要,先从Google网站申请一组经过验证的Maps API Key授权码,这个在网上很多package com.android.antking.gps;
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class Mymain extends Activity{
//定义一个LocationManager类
private LocationManager mLocationManager;
//LocationGPS主程序,实例化一个LocationManager对象mLocationManager
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLocationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
}
//在Resume阶段设定mLocationListener界面,可以获得地理位置的更新数据
@Override
protected void onResume() {
if (mLocationManager != null) {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
mLocationListener);
}
super.onResume();
}
//在Pause阶段关闭mLocationListener界面,不再获得地理位置的更新数据
@Override
protected void onPause() {
if (mLocationManager != null) {
mLocationManager.removeUpdates(mLocationListener);
}
super.onPause();
}
//实例化mLocationListener界面
public LocationListener mLocationListener = new LocationListener()
{
//GPS位置数据被更新
public void onLocationChanged(Location location) {
TextView mTextView01 = (TextView)findViewById(R.id.textView1);
TextView mTextView02 = (TextView)findViewById(R.id.textView2);
TextView mTextView03 = (TextView)findViewById(R.id.textView3);
TextView mTextView04 = (TextView)findViewById(R.id.textView4);
TextView mTextView05 = (TextView)findViewById(R.id.textView5);
TextView mTextView06 = (TextView)findViewById(R.id.textView6);
TextView mTextView07 = (TextView)findViewById(R.id.textView7);
mTextView01.setText("纬度-Latitude: " + String.valueOf(location.getLatitude()));
mTextView02.setText("经度-Longitude: " + String.valueOf(location.getLongitude()));
mTextView03.setText("精确度-Accuracy: " + String.valueOf(location.getAccuracy()));
mTextView04.setText("标高-Latitude: " + String.valueOf(location.getAltitude()));
mTextView05.setText("时间-Time: " + String.valueOf(location.getTime()));
mTextView06.setText("速度-Speed: " + String.valueOf(location.getSpeed()));
mTextView07.setText("方位-Bearing: " + String.valueOf(location.getBearing()));
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
//GPS位置数据的状态被更新
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case LocationProvider.AVAILABLE:
Log.v("Status", "AVAILABLE");
break;
case LocationProvider.OUT_OF_SERVICE:
Log.v("Status", "OUT_OF_SERVICE");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Log.v("Status", "TEMPORARILY_UNAVAILABLE");
break;
}
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <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:enabled="true" android:clickable="true" android:apiKey="0FZLYf-YM4SRrJrJum55MeeaO4Gd_IitVFmtUeA"/>自己的开发密钥 </LinearLayout>
在这个里面加入我们要用的google地图的包,还有Internet的权限
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.antking.map" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MyMain" 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"></uses-library> </application> </manifest>