Android中Google地图路径导航,使用mapfragment地图上画出线路(google map api v2)详解

分享到:

在这篇里我们只聊怎么在android中google map api v2地图上画出路径导航,用mapfragment而不是mapview,至于怎么去申请key,manifest.xml中加入的权限,系统中需要的google play services等另行查看资料,沦落凡间不详述。

参考:https://developers.google.com/maps/documentation/android/intro

   首先我们在Activity上加载一个GoogleMap,然后再在Map上画上标记和路径导航。

先上主要代码:

  1 public class DirectionActivity extends FragmentActivity {
  2     private Button btnBack;
  3     private SupportMapFragment mapFragment;
  4     private GoogleMap map;
  5     private LocationManager locationManager;
  6     private Location location;
  7     private List<LatLng> latlngs = new ArrayList<LatLng>();
  8     private double lat;
  9     private double lng;
 10     private String address;
 11     private double locLat;
 12     private double locLng;
 13     private LatLng locLatLng;
 14 
 15 
 16     @Override
 17     protected void onCreate(Bundle arg0) {
 18         super.onCreate(arg0);
 19         init();
 20         findViewById();
 21         setListenter();
 22         processLogic();
 23         getDirection();
 24     }
 25     
 26     /**
 27      * Get the direction
 28      */
 29     private void getDirection() {
 30         FoundRoadRouteTask task = new FoundRoadRouteTask(onFoundRoadRouteTaskListener);
 31         task.execute(new String[] {locLat+","+locLng, lat + "," + lng });
 32     }
 33 
 34     /**
 35      * Draw route information
 36      */
 37     private void drawRoute() {
 38         try {
 39             // Draw route info
 40             map.addMarker(new MarkerOptions().position(latlngs.get(0)).title(address).visible(true));
 41             PolylineOptions lineOptions = new PolylineOptions();
 42             lineOptions.width(5);
 43             for (int i = 0; i < latlngs.size() - 1; i++) {
 44                 lineOptions.add(latlngs.get(i));
 45             }
 46             map.addPolyline(lineOptions);
 47         } catch (Exception e) {
 48             
 49         }
 50     }
 51 
 52     private OnFounedRoadRouteListener onFoundRoadRouteTaskListener = new OnFounedRoadRouteListener() {
 53 
 54         @SuppressWarnings("unchecked")
 55         @Override
 56         public void found(Object obj) {
 57             if (obj != null) {
 58                 List<LatLng> tempLats = (List<LatLng>) obj;
 59                 latlngs.addAll(tempLats);
 60             }
 61             drawRoute();
 62         }
 63     };
 64 
 65     private void getLatLng() {
 66         try {
 67             Intent latlngIntent = getIntent();
 68             Bundle b = latlngIntent.getBundleExtra("latlng");
 69             lat = b.getDouble("lat");
 70             lng = b.getDouble("lng");
 71             address = b.getString("address");
 72         } catch (Exception e) {
 73             
 74         }
 75     }
 76 
 77     protected void init() {
 78         setContentView(R.layout.direction_layout);
 79         getLatLng();
 80         btnBack = (Button) this.findViewById(R.direction_layout.btn_back);
 81     }
 82 
 83     protected void processLogic() {
 84         
 85         locLat=application.getLatitude();
 86         locLng=application.getLongitude();
 87         
 88         FragmentManager manager = getSupportFragmentManager();
 89         mapFragment = (SupportMapFragment) manager
 90                 .findFragmentById(R.direction_layout.map);
 91         map = mapFragment.getMap();
 92         initMap();
 93     }
 94 
 95     protected void setListenter() {
 96         btnBack.setOnClickListener(onBackClickListener);
 97     }
 98 
 99     /**
100      * Initialize the map
101      */
102     private void initMap() {
103         try {
104         //设置在地图上显示手机的位置,和显示控制按钮
105             map.setMyLocationEnabled(true);
106             //设置室内地图是否开启
107             map.setIndoorEnabled(true);
108             //设置地图类型三种:1:MAP_TYPE_NORMAL: Basic map with roads.
109             //2:MAP_TYPE_SATELLITE: Satellite view with roads.3:MAP_TYPE_TERRAIN: Terrain view without roads.
110             map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
111             // map.setTrafficEnabled(true);
112             map.setOnMapLongClickListener(onMapLongClickListener);
113 
114             GPSUtil gpsUtil = new GPSUtil();
115             gpsUtil.startLocation(this, getContentResolver(),
116                     onLocationChangedListener);
117 
118             locationManager = (LocationManager) this
119                     .getSystemService(Context.LOCATION_SERVICE);
120             location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
121         } catch (Exception e) {
122             
123         }
124     }
125 
126     /** On Location change listener */
127     private OnLocationChangedListener onLocationChangedListener = new OnLocationChangedListener() {
128 
129         @Override
130         public void onLocationChanged(Location location) {
131             // To get the local latitude and longitude and set the map view to
132             // the position
133             if (location != null) {
134                 locLat = location.getLatitude();
135                 locLng = location.getLongitude();
136                 locLatLng = new LatLng(locLat, locLng);
137                 map.moveCamera(CameraUpdateFactory.newLatLng(locLatLng));
138                 map.moveCamera(CameraUpdateFactory.zoomTo(15));
139             }
140         }
141     };
142 
143     /** On Map long click listener */
144     private OnMapLongClickListener onMapLongClickListener = new OnMapLongClickListener() {
145 
146         @Override
147         public void onMapLongClick(LatLng latlng) {
148             Location location = new Location("LongPressLocationProvider");
149             location.setLatitude(latlng.latitude);
150             location.setLongitude(latlng.longitude);
151             //设置精度
152             location.setAccuracy(20);
153             onLocationChangedListener.onLocationChanged(location);
154         }
155     };
156 
157     private OnClickListener onBackClickListener = new OnClickListener() {
158 
159         @Override
160         public void onClick(View v) {
161             DirectionActivity.this.finish();
162         }
163     };
164 }
昵    称:
验证码:

相关文档: