高德地图——定位小作业
来源:互联网 发布:徐氏八字软件 编辑:程序博客网 时间:2024/06/05 10:20
作业要求如下:
利用地理围栏和天气接口做一个小APP,在你离开自己家的时候,获取一下天气信息。并且震动提醒
对地理围栏不了解的同学请参考
地理围栏
对查询天气不了解的同学请参考
查询天气
下面在地理围栏Demo的基础上,进行添加
这个也就是在获取用户离开地理围栏后,调用查询添加的接口查询一下添加,具体内容就不罗嗦了,大家参考上述实例就可以了,这里就就简单的贴一下完整代码。
完整代码如下:
package com.pansoft.oilgas.gaodenavigation;import android.Manifest;import android.app.PendingIntent;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.content.pm.PackageManager;import android.graphics.Color;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.os.PersistableBundle;import android.os.Vibrator;import android.support.annotation.NonNull;import android.support.v4.app.ActivityCompat;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.widget.TextView;import android.widget.Toast;import com.amap.api.location.AMapLocation;import com.amap.api.location.AMapLocationClient;import com.amap.api.location.AMapLocationClientOption;import com.amap.api.location.AMapLocationListener;import com.amap.api.maps.AMap;import com.amap.api.maps.AMapUtils;import com.amap.api.maps.CameraUpdateFactory;import com.amap.api.maps.LocationSource;import com.amap.api.maps.MapView;import com.amap.api.maps.model.CircleOptions;import com.amap.api.maps.model.LatLng;import com.amap.api.services.weather.LocalWeatherForecastResult;import com.amap.api.services.weather.LocalWeatherLive;import com.amap.api.services.weather.LocalWeatherLiveResult;import com.amap.api.services.weather.WeatherSearch;import com.amap.api.services.weather.WeatherSearchQuery;import java.lang.ref.WeakReference;public class SimpleGeoFenceActivity extends AppCompatActivity implements AMapLocationListener, LocationSource, WeatherSearch.OnWeatherSearchListener { final String tag = SimpleGeoFenceActivity.class.getSimpleName(); final int REQ_LOCATION = 0x12; final int REQ_GEO_FENCE = 0x13; final String ACTION_GEO_FENCE = "geo fence action"; private AMapLocationClient mLocationClient; @Override public void onWeatherLiveSearched(LocalWeatherLiveResult localWeatherLiveResult, int rCode) { if (rCode == 1000) { LocalWeatherLive liveWeather = localWeatherLiveResult.getLiveResult(); ShowWeatherFragment showFragment = ShowWeatherFragment.newInstance(liveWeather); showFragment.show(getFragmentManager(), "xxxx"); } } @Override public void onWeatherForecastSearched(LocalWeatherForecastResult localWeatherForecastResult, int rCode) { } private AMapLocationClientOption mLocationOption; private IntentFilter intentFilter; private Vibrator vibrator; private LatLng centerLatLng; private MapView mapView; private AMap aMap; private OnLocationChangedListener onLocationChangedListener; private String cityString; private GeoFenceHandler mHandler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mHandler = new GeoFenceHandler(this); mapView = new MapView(this); setContentView(mapView); mapView.onCreate(savedInstanceState); aMap = mapView.getMap(); vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); mLocationClient = new AMapLocationClient(this); mLocationOption = new AMapLocationClientOption(); mLocationClient.setLocationListener(this); mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); mLocationOption.setInterval(2000); mLocationClient.setLocationOption(mLocationOption); applyPermission(); //处理进出地理围栏事件 intentFilter = new IntentFilter(); intentFilter.addAction(ACTION_GEO_FENCE); //show my location aMap.setLocationSource(this); aMap.getUiSettings().setMyLocationButtonEnabled(true); aMap.setMyLocationEnabled(true); aMap.setMyLocationType(AMap.LOCATION_TYPE_MAP_FOLLOW); } static class GeoFenceHandler extends Handler { private WeakReference<SimpleGeoFenceActivity> weakReference; public GeoFenceHandler(SimpleGeoFenceActivity thisActivity) { weakReference = new WeakReference<>(thisActivity); } @Override public void handleMessage(Message msg) { if (weakReference != null && weakReference.get() != null) { SimpleGeoFenceActivity thisActivity = weakReference.get(); if (msg.what == 2) { //query weather WeatherSearchQuery weatherQuery = new WeatherSearchQuery( thisActivity.cityString, WeatherSearchQuery.WEATHER_TYPE_LIVE); WeatherSearch weatherSearch = new WeatherSearch( thisActivity); weatherSearch.setQuery(weatherQuery); weatherSearch.setOnWeatherSearchListener(thisActivity); weatherSearch.searchWeatherAsyn(); } } } } @Override protected void onResume() { super.onResume(); mapView.onResume(); this.registerReceiver(broadcastReceiver, intentFilter); } BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // 接收广播 if (intent.getAction().equals(ACTION_GEO_FENCE)) { Bundle bundle = intent.getExtras(); // 根据广播的event来确定是在区域内还是在区域外 int status = bundle.getInt("event"); String geoFenceId = bundle.getString("fenceId"); if (status == 1) {// Toast.makeText(SimpleGeoFenceActivity.this, "进入地理围栏~", Toast.LENGTH_LONG).show();// vibrator.vibrate(3000); } else if (status == 2) { // 离开围栏区域 Toast.makeText(SimpleGeoFenceActivity.this, "离开地理围栏~", Toast.LENGTH_LONG).show(); vibrator.vibrate(3000); mHandler.sendEmptyMessage(2); } } } }; @Override protected void onStop() { super.onStop(); this.unregisterReceiver(broadcastReceiver); } public void applyPermission() { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQ_LOCATION); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == REQ_LOCATION) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { mLocationClient.startLocation(); } else { Toast.makeText(SimpleGeoFenceActivity.this, "没有权限,无法获取位置信息~", Toast.LENGTH_LONG).show(); } } super.onRequestPermissionsResult(requestCode, permissions, grantResults); } @Override public void onLocationChanged(AMapLocation loc) { if (loc != null && loc.getErrorCode() == 0) { //设置地理围栏 if (centerLatLng == null) { centerLatLng = new LatLng(loc.getLatitude(), loc.getLongitude()); Intent intent = new Intent(ACTION_GEO_FENCE); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, REQ_GEO_FENCE, intent, PendingIntent.FLAG_UPDATE_CURRENT); //100:是围栏半径(测试发现,设置的太小,不会发出广播);-1:是超时时间(单位:ms,-1代表永不超时) mLocationClient.addGeoFenceAlert("fenceId", centerLatLng.latitude, centerLatLng.longitude, 100, -1, pendingIntent); addCircle(centerLatLng, 100); cityString = loc.getCity(); } else { if (onLocationChangedListener != null) { onLocationChangedListener.onLocationChanged(loc); } } } } public void addCircle(LatLng latLng, int radius) { CircleOptions circleOptions = new CircleOptions(); circleOptions.center(latLng); circleOptions.radius(radius); circleOptions.strokeWidth(4); circleOptions.strokeColor(Color.RED); circleOptions.fillColor(Color.BLUE); aMap.addCircle(circleOptions); } @Override protected void onPause() { super.onPause(); mapView.onPause(); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState); } @Override protected void onDestroy() { super.onDestroy(); mapView.onDestroy(); } @Override public void activate(OnLocationChangedListener onLocationChangedListener) { this.onLocationChangedListener = onLocationChangedListener; } @Override public void deactivate() { if (mLocationClient != null) { mLocationClient.stopLocation(); mLocationClient.onDestroy(); } }}
1 0
- 高德地图——定位小作业
- Android 高德地图——地图定位
- 高德地图定位
- 高德地图---定位
- 高德地图定位
- 高德地图定位
- 高德地图 - 定位
- 高德地图定位
- 高德地图-定位
- 高德地图定位
- 高德地图定位
- 高德地图定位
- 高德地图定位
- 高德地图定位
- 高德地图定位
- 高德地图定位
- 高德地图定位
- 百度地图定位,高德地图定位
- Java Socket编程中处理长连接的方法
- android中的文件操作详解以及内部存储和外部存储
- C程序员之终极标靶
- 手机设计
- ipython notebook 远程访问
- 高德地图——定位小作业
- Homebrew backup
- WCDMA终端网络搜索过程剖析
- linux常用命令
- 如何发布Web项目到互联网
- acm之递归题目9
- enum 关键字
- java Object类
- Android分包MultiDex原理详解