1、 /位置信息更新时调用 public void onLocationChanged(Location location) if (location != null) Toast.makeText(getBaseContext(), New location latitude + location.getLatitude() + longitude location.getLongitude()+, Toast.LENGTH_SHORT).show(); /当禁用的提供程序被调用时会立即调用onProviderDisabled(String provider) /用户启用提供程序时被调用onPr
2、oviderEnabled(String provider) /状态变化时被调用onStatusChanged(String provider, int status, Bundle extras) ;startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCALE_SETTINGS), 0);/请求用户打开GPS/*LocationManager 接近提醒*/geo模式:用uri的前缀来过滤数据类型String PROX_ALERT = com.androidbook.intent.action.PROXIMI
3、TY_ALERT;IntentFilter iFilter = new IntentFilter(PROX_ALERT);/设置了过滤器所接受的行为iFilter.addDataScheme(geo);/设置了过滤器说接受的uri前缀String geo = geo:+lat+,+lon;Intent intent = new Intent(PROX_ALERT, Uri.parse(geo);/在intent中添加uri匹配intentFilterpIntent1 = PendingIntent.getBroadcast(getApplicationContext(), 0, intent,
4、 PendingIntent.FLAG_CANCEL_CURRENT);/创建PendingIntentlocMgr.addProximityAlert(lat, lon, radius, 6000L, pIntent1);/纬度,经度,半径,超时,触发后的行为AndroidManifest.xmlmanifest xmlns:android= package=com.example.v2_test android:versionCode=1versionName= permissionname=com.example.v2_test.permission.MAPS_RECEIVEprotec
5、tionLevel=signature uses-permission android:android.permission.INTERNETandroid.permission.WRITE_EXTERNAL_STORAGEcom.google.android.providers.gsf.permission.READ_GSERVICESandroid.permission.ACCESS_COARSE_LOCATIONandroid.permission.ACCESS_FINE_LOCATIONandroid.permission.ACCESS_MOCK_LOCATION/uses-featu
6、reglEsVersion=0x00020000required=trueapplicationallowBackup=icon=drawable/ic_launcherlabel=string/app_nametheme=style/AppThemeactivitycom.example.v2_test.MainActivityintent-filteraction android:android.intent.action.MAINcategory android:android.intent.category.LAUNCHER/intent-filter/activitymeta-dat
7、acom.google.android.maps.v2.API_KEYvalue=AIzaSyDmrnUUl_zWzgimKKeR5t3ZiinvouSWO8E/application/manifest布局文件RelativeLayout xmlns: xmlns:map=layout_width=match_parentlayout_height=fragmentid=+id/map class=com.google.android.gms.maps.SupportMapFragmentImageButton+id/localbackground=nullwrap_contentlayout
8、_marginTop=10dplayout_alignParentRight=layout_marginRight=/RelativeLayout菜单弹出框布局文件TableLayout xmlns:fill_parentorientation=verticalTextViewtext=起始位置:textColor=android:color/whitetextSize=18dp/TextViewEditText+id/start我的位置/EditText目的地:+id/end/TableLayout这个是activitypackage com.example.v2_test;import j
9、ava.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.
10、http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import com.google.android.gms.maps.CameraUpdateF
11、actory;import com.google.android.gms.maps.GoogleMap;import com.google.android.gms.maps.SupportMapFragment;import com.google.android.gms.maps.model.CameraPosition;import com.google.android.gms.maps.model.LatLng;import com.google.android.gms.maps.model.Marker;import com.google.android.gms.maps.model.M
12、arkerOptions;import com.google.android.gms.maps.model.Polyline;import com.google.android.gms.maps.model.PolylineOptions;import android.app.AlertDialog;import android.app.ProgressDialog;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.graphi
13、cs.Color;import android.location.Criteria;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.v4.app.FragmentActivity;import android.util.
14、Log;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.EditText;import android.widget.ImageButton;public class MainActivity extends FragmentActivity List list; Polyline polyline; private Location location; private C
15、ameraPosition cameraPosition; private GoogleMap map; String start = String end = double lat = 0.0; double lng = 0.0; double endlat = 0.0; double endlng = 0.0; LocationManager locationManager; ImageButton local; LocationListener llistener; Marker marker; private ProgressDialog progressdialog; private
16、 Handler mHandler = new Handler() public void handleMessage(Message msg) switch (msg.what) case 1: progressdialog.dismiss(); if(polyline!=null) polyline.remove(); map.clear(); markMysition(); map.addMarker(new MarkerOptions().position( new LatLng(endlat, endlng).title( MainActivity.this.getLocation(
17、endlat, endlng); LatLng last = null; for (int i = 0; i list.size() - 1; i+) LatLng src = list.get(i); LatLng dest = list.get(i + 1); last = dest; polyline = map.addPolyline(new PolylineOptions() .add(new LatLng(src.latitude, src.longitude), new LatLng(dest.latitude, dest.longitude) .width(4).color(C
18、olor.GREEN); break; ; Override protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); local = (ImageButton) findViewById(R.id.local); local.setImageResource(R.drawable.ww); / 点击按钮视图回到我的位置 local.setOnClickListener(new android.vie
19、w.View.OnClickListener() Override public void onClick(View v) MainActivity.this.setCameraPosition(); ); map = (SupportMapFragment) (this.getSupportFragmentManager() this.getPointLocation(); MainActivity.this.setCameraPosition(); / 标记我的位置 void markMysition() marker=map.addMarker(new MarkerOptions().position(new LatLng(lat, lng).title( this.getLocation(lat,