一.GPS定位:android 的三种定位方式
二.GPS常用的类:
二.GPS定位代码:
三.使用高德地图获取定位数据:官网:https://lbs.amap.com
四.使用高德地图:官网:https://lbs.amap.com
一.GPS定位:android 的三种定位方式
1.GPS定位:需要GPS硬件支持,直接和卫星交互来获取当前经纬度。
优点:速度快、精度高、可在无网络情况下使用。
缺点:首次连接时间长、只能在户外已经开阔地使用,设备上方有遮挡物就不行了、比较耗电。
2.Network定位:又细分为WIFI定位和基站定位
(1)基站定位:一般手机附近的三个基站进行三角定位,由于每个基站的位置是固定的,利用电磁波在这三个基站间中转所需要时间来算出手机所在的坐标
a.优点:受环境的影响情况较小,不管在室内还是人烟稀少的地方都能用,只要有基站。
b.缺点:首先需要消耗流量、其实精度没有GPS那么准确,大概在十几米到几十米之间
(2)WIFI定位:
a.优点:和基站定位一样,它的优势在于收环境影响较小,只要有Wifi的地方可以使用。
b.缺点:需要有wifi、精度不准
3.AGPS定位:AssistedGPS(辅助全球卫星定位系统),是结合GSM或GPRS与传统卫星定位
二.GPS常用的类:
LocationManager:位置信息管理类。
Android为定位系统提供了LocationManager管理类。通过LocationManager和其他几个辅助类,开发人员可以方便开发出GPS应用:
LocationManager lm = getSystemService(Context.LOCATION_SERVICE);
LocationProvider: 提供定位信息的类。
LocationManager 提供了方法getAllProviders()获取所有的LocationProvider;
也可以通过名称获取指定的LocationProvider。
(1) GPS定位:通过卫星实现的
LocationProvider lProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER);
(2) 网络定位:通过WI-FI 或者信号塔进行定位
LocationProvider lProvider = locationManager.getProvider(LocationManager.NETWORK_PROVIDER);
Location:位置类
double getLatitude()获取维度值
double getLongtitude()获取经度值
double getAltitude(); 获得海拔
在设备位置发生改变的时候获取到最新的位置信息。
void requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, new LocationListener{
void onLocationChanged();
}) 获取GPS定位信息。
第一个参数:位置提供器的类型。
第二个参数:监听位置变化的时间间隔,以毫秒为单位。
第三个参数:监听位置变化的距离间隔,以米为单位。
第四个参数:LocationListener 监听器。
如上,LocationManager 每隔 3 秒钟会检测一下位置的变化情况,当移动距离超过 3000米的时候,就会调用 LocationListener 的 onLocationChanged() 方法,并把新的位置信息作为参数传入
实现代码
package com.example.day09_gdmap;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.MyLocationStyle;
import com.amap.api.maps2d.overlay.PoiOverlay;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
String[] strings=new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_SMS,Manifest.permission.INTERNET,
Manifest.permission.READ_CALL_LOG, Manifest.permission.WRITE_CALL_LOG,
Manifest.permission.READ_CONTACTS,Manifest.permission.WRITE_CONTACTS,
Manifest.permission.RECEIVE_SMS,Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_NETWORK_STATE,Manifest.permission.ACCESS_WIFI_STATE,Manifest.permission.READ_PHONE_STATE
,Manifest.permission.ACCESS_COARSE_LOCATION
};
MapView map;
AMap aMap;
MyLocationStyle myLocationStyle;
Button button;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
for (int i = 0; i < strings.length; i++) {
String string = strings[i];
int reust = ActivityCompat.checkSelfPermission(this, string);
if (reust!= PackageManager.PERMISSION_GRANTED){
requestPermissions(strings,1001);
break;
}
}
}
setContentView(R.layout.activity_main);
button = findViewById(R.id.find);
et = findViewById(R.id.et);
map = findViewById(R.id.map_view);
map.onCreate(savedInstanceState);
aMap = map.getMap();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = et.getText().toString();
if (TextUtils.isEmpty(text)){
Toast.makeText(MainActivity.this, "不能为空", Toast.LENGTH_SHORT).show();
}
PoiSearch.Query query = new PoiSearch.Query(text,"","北京市");
query.setPageNum(1);//分页显示
query.setPageSize(10);//返回数量
PoiSearch poiSearch = new PoiSearch(MainActivity.this,query);
poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
@Override
public void onPoiSearched(PoiResult poiResult, int i) {
if (i == 1000){
ArrayList<PoiItem> pois = poiResult.getPois();
aMap.clear();
PoiOverlay poiOverlay = new PoiOverlay(aMap, pois);
poiOverlay.removeFromMap();
poiOverlay.addToMap();
poiOverlay.zoomToSpan();
}
}
@Override
public void onPoiItemSearched(PoiItem poiItem, int i) {
}
});
poiSearch.searchPOIAsyn();
}
});
intLocation();
}
private void intLocation() {
myLocationStyle = new MyLocationStyle();
myLocationStyle.interval(2000);
aMap.setMyLocationStyle(myLocationStyle);
aMap.setMyLocationEnabled(true);
}
@Override
protected void onDestroy() {
super.onDestroy();
map.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
map.onResume();
}
@Override
protected void onPause() {
super.onPause();
map.onPause();
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
map.onSaveInstanceState(outState);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
路线代码
package com.example.homework_7_15_1;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.MyLocationStyle;
import com.amap.api.maps2d.overlay.DrivingRouteOverlay;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RideRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.WalkRouteResult;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ListView list;
Button bt;
EditText et;
String[] data;
ArrayList<PoiItem> lists = new ArrayList<>();
ArrayAdapter adapter;
MapView map;
AMap aMap;
LatLonPoint start=new LatLonPoint(0,0);//经纬度
LatLonPoint end=new LatLonPoint(0,0);//经纬度
MyLocationStyle myLocationStyle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = findViewById(R.id.map);
list = findViewById(R.id.list);
bt = findViewById(R.id.find);
et = findViewById(R.id.et);
map.onCreate(savedInstanceState);
aMap = map.getMap();
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.setVisibility(View.VISIBLE);
map.setVisibility(View.GONE);
String mes = et.getText().toString();
PoiSearch.Query query = new PoiSearch.Query(mes,"","北京市");
query.setPageSize(2000);
query.setPageNum(1);
PoiSearch poiSearch = new PoiSearch(MainActivity.this, query);
poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
@Override
public void onPoiSearched(PoiResult poiResult, int i) {
if (i == 1000){
ArrayList<PoiItem> pois = poiResult.getPois();
lists.addAll(pois);
data = new String[pois.size()];
for (int j = 0; j < pois.size(); j++) {
data[j] = pois.get(j).getTitle();
}
adapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item,data);
}
}
@Override
public void onPoiItemSearched(PoiItem poiItem, int i) {
}
});
poiSearch.searchPOIAsyn();
list.setAdapter(adapter);
}
});
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
list.setVisibility(View.GONE);
map.setVisibility(View.VISIBLE);
// myStyle();
// String item = data[position];
// String mes = et.getText().toString();
// PoiSearch.Query query = new PoiSearch.Query(item,"","北京市");
// query.setPageSize(2000);
// query.setPageNum(1);
// PoiSearch poiSearch = new PoiSearch(MainActivity.this, query);
// poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
// @Override
// public void onPoiSearched(PoiResult poiResult, int i) {
// if (i==1000){
// ArrayList<PoiItem> pois = poiResult.getPois();
// aMap.clear();
// PoiOverlay poiOverlay = new PoiOverlay(aMap, pois);
// poiOverlay.removeFromMap();
// poiOverlay.addToMap();
// poiOverlay.zoomToSpan();
// }
// }
// @Override
// public void onPoiItemSearched(PoiItem poiItem, int i) {
//
// }
// });
// poiSearch.searchPOIAsyn();
//TODO 1:创建RouteSearch对象
RouteSearch routeSearch = new RouteSearch(MainActivity.this);
//TODO 2:设置监听,得到结果,画出路线
routeSearch.setRouteSearchListener(new RouteSearch.OnRouteSearchListener() {
@Override
public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {
}
@Override
public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {
//获得路径的集合
List<DrivePath> paths = driveRouteResult.getPaths();
//画驾驶路线
DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(
MainActivity.this, aMap, paths.get(0),
start,
end, null);
//设置节点marker是否显示:
// drivingRouteOverlay.setNodeIconVisibility(true);
// drivingRouteOverlay.setThroughPointIconVisibility(true);
drivingRouteOverlay.removeFromMap();//将之前的路径移除
drivingRouteOverlay.addToMap();//画上新的路径
drivingRouteOverlay.zoomToSpan();//放大
}
@Override
public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {
}
@Override
public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {
}
});
//TODO 3:设置参数
//当前用户的位置
double longitude = aMap.getMyLocation().getLongitude();//精度
double latitude = aMap.getMyLocation().getLatitude();//维度
start.setLongitude(longitude);
start.setLatitude(latitude);
//获得用户选择的重点
PoiItem poiItem = lists.get(position);
end= poiItem.getLatLonPoint();
RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(start,end);
RouteSearch.DriveRouteQuery driveRouteQuery = new RouteSearch.DriveRouteQuery(fromAndTo,0,null,null,"");
//TODO 4:计算
routeSearch.calculateDriveRouteAsyn(driveRouteQuery);
}
});
}
private void myStyle() {
myLocationStyle = new MyLocationStyle();
myLocationStyle.interval(2000);
aMap.setMyLocationStyle(myLocationStyle);
aMap.setMyLocationEnabled(true);
}
@Override
protected void onDestroy() {
super.onDestroy();
map.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
map.onResume();
}
@Override
protected void onPause() {
super.onPause();
map.onPause();
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
map.onSaveInstanceState(outState);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201