安卓开发笔记①:利用高德地图API进行定位、开发电子围栏、天气预报、轨迹记录、搜索周边(位置)
本文只是开发小笔记(关键步骤详见其它略讲),有任何不懂的问题可以留言,我必为大家解答以及分享更有用的经验。
△
高德地图开发时需要导入的包在下面的网盘链接中:(由于高德地图api更新得太快,官网上最新的包使用起来没有之前的方便,所以以下提供最全面的原始包)
链接:http://pan.baidu.com/s/1eR8Nchc 密码:be1v
其中armeabi这个文件夹和其他4个jar是必要导入的,另外两个文件夹确保兼容性。
①首先登陆高德lbs开放平台获取Key,下面是官方详细教程:
http://lbs.amap.com/dev/ticket#/faqcat/1
(本人使用的是eclipse,所以获取安全码SHA1的方法为Windows->preferences->android->build,如下图所示的SHA1 fingerprint)
然后你要设置你所开发的app的包,即在下面的Package中输入你的app包名(这点不能输错)
②申请到key之后,修改安卓项目的AndroidManifest.xml文件,即获取相关的权限和导入key(才能正常启动高德地图)
11个<uses-permission….>就是相关权限的设置,把代码写进去
还有加一个<meta-data….>把你的key导入进去,需要记住android:name=”com.amap.api.v2.apikey”
下面的代码都是调试所用,所有信息都将在logcat栏目里面显示。
③进行定位:
package (你的包名);
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.maps.model.MyLocationStyle;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity implements AMapLocationListener {
LocationManagerProxy mLocationManagerProxy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocationManagerProxy=LocationManagerProxy.getInstance(this);
mLocationManagerProxy.requestLocationData(LocationProviderProxy.AMapNetwork, 2000, 15, this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mLocationManagerProxy.destroy();
}
@Override
public void onLocationChanged(AMapLocation arg0) {
// TODO Auto-generated method stub
if(arg0!=null&&arg0.getAMapException().getErrorCode()==0){
Log.i("helloworld", arg0.toString());
}
}
④开发电子围栏(也就是设置一个点,可以设置半径,可以监控自己的位置是在圈内还是圈外)
package (你的包名);
import com.amap.api.maps.AMap.OnMapClickListener;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.CircleOptions;
import com.amap.api.maps.model.LatLng;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
public class GeoFenceActivity extends Activity implements OnMapClickListener,AMapLocationListener{
LocationManagerProxy mLocationManagerProxy;
MapView mapView;
String GEOFENCE_BROADCAST_ACTION="com.example.helloworld";
PendingIntent mPendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocationManagerProxy=LocationManagerProxy.getInstance(this);
mapView=(MapView)findViewById(R.id.main_mapView);
mapView.onCreate(savedInstanceState);
mapView.getMap().setOnMapClickListener(this);
Intent intent=new Intent(GEOFENCE_BROADCAST_ACTION);
mPendingIntent=PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
IntentFilter intentfilter=new IntentFilter();
intentfilter.addAction(GEOFENCE_BROADCAST_ACTION);
this.registerReceiver(mGeoFenceReceiver, intentfilter);
mLocationManagerProxy.requestLocationData(LocationManager.GPS_PROVIDER,2000,15, this);
}
private BroadcastReceiver mGeoFenceReceiver=new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.e("helloworld", "收到广播");
int i= intent.getIntExtra("status", -1);
if(i==1){
Log.e("helloworld", "在地理围栏内部");
}
if(i==0){
Log.e("helloworld", "在地理围栏外部");
}
}
};
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mapView.onResume();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public void onMapClick(LatLng arg0) {
// TODO Auto-generated method stub
Log.e("helloworld", "lat ="+arg0.latitude);
Log.e("helloworld", "lon ="+arg0.longitude);
mapView.getMap().addCircle(new CircleOptions().center(arg0).radius(1000));
mLocationManagerProxy.addGeoFenceAlert(arg0.latitude, arg0.longitude, 1000, 1000*60*30, mPendingIntent);
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(AMapLocation arg0) {
// TODO Auto-generated method stub
}
}
⑤天气预报(现在或未来的):
package (你的包名);
import java.util.List;
import com.amap.api.location.AMapLocalDayWeatherForecast;
import com.amap.api.location.AMapLocalWeatherForecast;
import com.amap.api.location.AMapLocalWeatherListener;
import com.amap.api.location.AMapLocalWeatherLive;
import com.amap.api.location.LocationManagerProxy;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class WeatherActivity extends Activity implements AMapLocalWeatherListener{
LocationManagerProxy mLocationManagerProxy;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocationManagerProxy=LocationManagerProxy.getInstance(this);
mLocationManagerProxy.requestWeatherUpdates(LocationManagerProxy.WEATHER_TYPE_FORECAST,this);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public void onWeatherForecaseSearched(AMapLocalWeatherForecast arg0) {
// TODO Auto-generated method stub
//未来天气
List<AMapLocalDayWeatherForecast> list=arg0.getWeatherForecast();
for(int i=0;i<list.size();i++){
AMapLocalDayWeatherForecast dayweather=list.get(i);
Log.i("helloworld", "城市"+dayweather.getCity());
Log.i("helloworld", "时间"+dayweather.getDate());
Log.i("helloworld", "温度"+dayweather.getDayTemp());
Log.i("helloworld", "风力"+dayweather.getDayWindPower());
}
}
@Override
public void onWeatherLiveSearched(AMapLocalWeatherLive arg0) {
// TODO Auto-generated method stub
//当天天气
Log.i("helloworld", "城市"+arg0.getCity());
Log.i("helloworld", "湿度"+arg0.getHumidity());
Log.i("helloworld", "风力"+arg0.getWindPower());
}
}
⑥轨迹记录(这里是有模拟轨迹数据,如果是真正的设备的话可以把每次定位的数据放入容器中再读取轨迹)
package com.example.maptest;
import java.util.ArrayList;
import java.util.List;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap.OnMapLoadedListener;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.PolygonOptions;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.os.Trace;
public class TrackActivity extends Activity implements OnMapLoadedListener{
MapView mapview;
double Trace[]={
40.03833763826341,116.44161604271481,
40.038120,116.441789,
40.0378823754,116.441780,
40.037589,116.441768,
40.03744051,116.44175,
40.036884,116.441722,
40.03663242,116.441705466};
List<LatLng>list;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapview=(MapView)findViewById(R.id.main_mapView);
mapview.onCreate(savedInstanceState);
mapview.getMap().setOnMapLoadedListener(this);
list=new ArrayList<LatLng>();
for(int i=0;i<Trace.length-1;i++){
LatLng latlng=new LatLng(Trace[i], Trace[++i]);
list.add(latlng);
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mapview.onResume();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public void onMapLoaded() {
// TODO Auto-generated method stub
mapview.getMap().addPolygon(new PolygonOptions().addAll(list));
}
}
⑦搜索周边位置(以下例子是将搜索范围确定在“杨小贤”,“餐饮”,“广州市”)并且是以自己所在大学城位置为中心半径为200000m范围内搜索:
package (你的包名);
import java.util.List;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.maps.MapView;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiItemDetail;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.amap.api.services.poisearch.PoiSearch.OnPoiSearchListener;
import com.amap.api.services.poisearch.PoiSearch.SearchBound;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SearchActivity extends Activity implements AMapLocationListener,OnPoiSearchListener,OnClickListener {
LocationManagerProxy mLocationManagerProxy;
MapView mapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView=(MapView)findViewById(R.id.main_mapView);
mapView.onCreate(savedInstanceState);
mLocationManagerProxy=LocationManagerProxy.getInstance(this);
mLocationManagerProxy.requestLocationData(LocationProviderProxy.AMapNetwork, 2000, 15, this);
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
AMapLocation location;
@Override
public void onLocationChanged(AMapLocation arg0) {
// TODO Auto-generated method stub
location=arg0;
Log.e("helloworld", arg0.toString());
}
@Override
public void onPoiItemDetailSearched(PoiItemDetail arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onPoiSearched(PoiResult arg0, int arg1) {
// TODO Auto-generated method stub
if(arg1==0){
List<PoiItem>list=arg0.getPois();
for(int i=0;i<list.size();i++){
PoiItem item=list.get(i);
Log.e("helloworld", item.toString());
}
}
}
public void search(){
PoiSearch.Query query=new PoiSearch.Query("杨小贤", "餐饮", "广州市");
query.setPageSize(10);//每页显示多少个poiitem
query.setPageNum(0);
PoiSearch poiSearch=new PoiSearch(this, query);
poiSearch.setOnPoiSearchListener(this);
LatLonPoint poinot=new LatLonPoint(location.getLatitude(), location.getLongitude());
poiSearch.setBound(new SearchBound(poinot, 200000, true));
poiSearch.searchPOIAsyn();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
search();
}
}
转载自:https://blog.csdn.net/CSDN568302203/article/details/52403706