1. 程式人生 > >最新基於高德地圖的android進階開發(4)GPS定位之POI(Point of interesting)興趣點原始碼簡介

最新基於高德地圖的android進階開發(4)GPS定位之POI(Point of interesting)興趣點原始碼簡介

1.跑了一下高德提供的POI的原始碼,感覺還是比較簡單的,沒有達到想要的效果,還有改進的空間,後面會繼續更新。

2.下面直接上程式碼,已經註釋好了

main.java

package com.dragon.arnav.basicFuction.poi;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import
com.amap.api.maps.AMapUtils; import com.amap.api.maps.MapView; import com.amap.api.maps.model.BitmapDescriptorFactory; import com.amap.api.maps.model.Marker; import com.amap.api.maps.model.MarkerOptions; import com.amap.api.maps.model.NaviPara; import com.amap.api.maps.model.Poi; import com.dragon.arnav.R; /** * This file created by dragon on 2016/7/19 15:15,belong to com.dragon.arnav.basicFuction.poi . */
public class PoiClick extends Activity implements AMap.OnPOIClickListener,AMap.OnMarkerClickListener { private MapView mMapView; private AMap mAMap; protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.poiclick); mMapView = (MapView) findViewById(R.id.map); mMapView.onCreate(savedInstanceState); mAMap = mMapView.getMap(); // 設定監聽時間
mAMap.setOnPOIClickListener(this); mAMap.setOnMarkerClickListener(this); } //地圖POI點選回撥 @Override public void onPOIClick(Poi poi){ mAMap.clear(); MarkerOptions markerOptions = new MarkerOptions(); // 獲取座標 markerOptions.position(poi.getCoordinate()); // 下面就是用來顯示一個圖示 TextView textView = new TextView(getApplicationContext()); textView.setText("到"+poi.getName()+"去"); textView.setGravity(Gravity.CENTER); textView.setTextColor(Color.BLACK); textView.setBackgroundResource(R.drawable.custom_info_bubble); markerOptions.icon(BitmapDescriptorFactory.fromView(textView)); mAMap.addMarker(markerOptions); } // 點選Marker圖示後的執行的操作 @Override public boolean onMarkerClick(Marker marker){ // 構造導航引數 NaviPara naviPara = new NaviPara(); // 設定終點位置 naviPara.setTargetPoint(marker.getPosition()); // 設定導航策略,這裡是避免擁堵 naviPara.setNaviStyle(AMapUtils.DRIVING_AVOID_CONGESTION); try{ // 這裡呼叫高德地圖來導航 AMapUtils.openAMapNavi(naviPara,getApplicationContext()); } catch (com.amap.api.maps.AMapException e){ // 如果你沒安裝,這會觸發這個異常 AMapUtils.getLatestAMapApp(getApplicationContext()); } mAMap.clear(); return false; } // 下面這些沒什麼要說的 @Override protected void onResume(){ super.onResume(); mMapView.onResume(); } @Override protected void onPause(){ super.onPause(); mMapView.onPause(); } @Override protected void onSaveInstanceState(Bundle outState){ super.onSaveInstanceState(outState); mMapView.onSaveInstanceState(outState); } @Override protected void onDestroy(){ super.onDestroy(); mMapView.onDestroy(); } }

main.xml

<?xml version="1.0" encoding="utf-8"?>
<com.amap.api.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</com.amap.api.maps.MapView>

3.注意添相應的許可權,下面是效果圖。

這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述