Android版高德导航应用开发小结

/

软硬件环境

  • Macbook Pro MGX 72
  • Android Studio 1.3.1
  • 红米Note增强版

准备工作

  • 上高德官网LBS开放平台注册帐户
  • 下载SDK和Demo程序
    地址: http://lbs.amap.com/api/android-navi-sdk/down/
  • 给自己的应用申请Key
    这个具体步骤高德的官网上都有详细的说明及操作步骤,就不多说了,照着做就行了

    djamap_01.png

Android Studio运行高德导航Demo

修改AndroidManifest.xml

  1. <meta-data
  2. android:name="com.amap.api.v2.apikey"
  3. android:value="4e8ee81efa8c19c8d9e0b55761f6962d" />
  4. <uses-permission android:name="android.permission.INTERNET" />
  5. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  6. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  7. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  8. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  9. <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  10. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
  11. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  12. <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
  13. <uses-permission android:name="android.permission.WAKE_LOCK" />
  14. <uses-permission android:name="android.permission.WRITE_SETTINGS" />
  15. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

常见错误及解决办法

  • android SDK V4和V7冲突?

djamap_02.png
首先在app下的build.gradle文件中删除provided files(‘libs/android-support-v4.jar’)或者通过File菜单下的Project Structure中的app模块中的Dependencies选项删除。然后在工程libs目录下删除android-support-v4.jar文件。

  • 工程运行后报TTSController初始化失败?
    我的做法是把libs中的so库全部copy出来,放在jniLibs文件夹中,jniLibs是自己创建的,跟libs在同一层目录下(其它目录也可以),然后在build.gradle中声明下,以下这段语句放在android标签下即可

    1. sourceSets {
    2. main {
    3. jniLibs.srcDirs = ['jniLibs']
    4. }
    5. }

大功告成,来张图秀一下

djamap_03.png

进行一次定位

  1. private LocationManagerProxy mLocationManager;
  2. private AMapLocationListener mLocationListener = new AMapLocationListener() {
  3. @Override
  4. public void onStatusChanged(String provider, int status, Bundle extras) {
  5. // TODO Auto-generated method stub
  6. }
  7. @Override
  8. public void onProviderEnabled(String provider) {
  9. // TODO Auto-generated method stub
  10. }
  11. @Override
  12. public void onProviderDisabled(String provider) {
  13. // TODO Auto-generated method stub
  14. }
  15. @Override
  16. public void onLocationChanged(Location location) {
  17. // TODO Auto-generated method stub
  18. }
  19. @Override
  20. public void onLocationChanged(AMapLocation location) {
  21. if (location != null && location.getAMapException().getErrorCode() == 0) {
  22. Log.e(TAG, "GPS: " + location.getLatitude() + "," + location.getLongitude());
  23. LatLng latLng = new LatLng(mStartPoint.getLatitude(), mStartPoint.getLongitude());
  24. MarkerOptions markerOptions = new MarkerOptions();
  25. markerOptions.position(latLng);
  26. markerOptions.title("您当前的位置");
  27. mAmap.addMarker(markerOptions);
  28. mAmap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 12));
  29. } else {
  30. showToast("定位出现异常,请检查网络连接.");
  31. mGPSProgressDialog.dismiss();
  32. //以上海市人民政府为中心显示定位时出错的初始地图
  33. LatLng latLng = new LatLng(31.230429, 121.473709);
  34. mAmap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 12));
  35. }
  36. }
  37. };
  38. mLocationManager = LocationManagerProxy.getInstance(this);
  39. mLocationManger.requestLocationData(LocationProviderProxy.AMapNetwork, -1, 15, mLocationListener);

兴趣点(Point Of Interest)查询

应用场景一般是这样的: 在输入框中输入关键字如”东方明珠”,系统则提供所有跟”东方明珠”相关的地址信息供用户选择。以下代码是在一个输入框中输入一字符串,经过查询POI Search将相关结果返回,以listview的形式显示出来,经用户点击后将具体的信息写入到输入框中。原来项目需要,这里还记录了对应地址的经纬度数据。

  1. //输入框是一个EditText
  2. mEndPointText.addTextChangedListener(new TextWatcher() {
  3. @Override
  4. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  5. }
  6. @Override
  7. public void onTextChanged(CharSequence s, int start, int before, int count) {
  8. String newText = s.toString().trim();
  9. PoiSearch.Query query = new PoiSearch.Query(newText, "", "021");
  10. query.setPageSize(10);
  11. query.setPageNum(1);
  12. PoiSearch poiSearch = new PoiSearch(NaviStartActivity.this, query);
  13. poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
  14. @Override
  15. public void onPoiSearched(PoiResult poiResult, int i) {
  16. if (i == 0) {
  17. for (int j = 0; j < poiResult.getPois().size(); j++) {
  18. if (!poiResult.getPois().get(j).getSnippet().isEmpty()) {
  19. poiAddressList.add(poiResult.getPois().get(j).getSnippet());
  20. poiLatLonList.add(poiResult.getPois().get(j).getLatLonPoint());
  21. }
  22. }
  23. poiAddressArray = new String[poiAddressList.size()];
  24. poiAddressArray = poiAddressList.toArray(poiAddressArray);
  25. poiLatLngArray = new LatLonPoint[poiLatLonList.size()];
  26. poiLatLngArray = poiLatLonList.toArray(poiLatLngArray);
  27. adapterTextview_poi_result = new AdapterTextview(NaviStartActivity.this, poiAddressArray);
  28. listView_poi_result.setAdapter(adapterTextview_poi_result);
  29. adapterTextview_poi_result.notifyDataSetChanged();
  30. poiAddressList.clear();
  31. poiLatLonList.clear();
  32. }
  33. }
  34. @Override
  35. public void onPoiItemDetailSearched(PoiItemDetail poiItemDetail, int i) {
  36. }
  37. });
  38. listView_poi_result.setOnItemClickListener(new OnItemClickListener() {
  39. @Override
  40. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  41. mEndPoints.clear();
  42. mEndPointText.setText(poiAddressArray[position]);
  43. mEndPoint = new NaviLatLng(poiLatLngArray[position].getLatitude(), poiLatLngArray[position].getLongitude());
  44. mEndPoints.add(mEndPoint);
  45. }
  46. });
  47. poiSearch.searchPOIAsyn();
  48. }
  49. @Override
  50. public void afterTextChanged(Editable s) {
  51. }
  52. });

地理编码

地理编码,通俗来讲就是将具体的地址(如xx区xx路)转换成经纬度数据,来看代码

  1. GeocodeSearch geocodeSearch = new GeocodeSearch(this);
  2. geocodeSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
  3. @Override
  4. public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
  5. //逆地理编码,经纬度转换成地址
  6. }
  7. @Override
  8. public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
  9. //地理编码,地址转换成经纬度
  10. if (i == 0) {
  11. if (geocodeResult != null && geocodeResult.getGeocodeAddressList() != null && geocodeResult.getGeocodeAddressList().size() > 0) {
  12. GeocodeAddress geocodeAddress = geocodeResult.getGeocodeAddressList().get(0);
  13. Log.e(TAG, "destination position: " + geocodeAddress.getLatLonPoint());
  14. }
  15. } else {
  16. Toast.makeText(NaviStartActivity.this, "该地址不存在!", Toast.LENGTH_SHORT).show();
  17. }
  18. }
  19. });
  20. //以上海地区地址的查询为例,城市代码可以查看高德的文档
  21. GeocodeQuery geocodeQuery = new GeocodeQuery(address, "021");
  22. geocodeSearch.getFromLocationNameAsyn(geocodeQuery);

完整导航Demo的下载地址

http://download.csdn.net/detail/djstavav/9016909

Github地址

https://github.com/djstava/DJAmap

转载请注明作者和出处,并添加本页链接。
原文链接:http://xugaoxiang.com/post/79

给我留言