第一次提交
This commit is contained in:
10
location/src/main/AndroidManifest.xml
Normal file
10
location/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1,10 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.fengliyan.location" >
|
||||
<application android:allowBackup="true" android:label="@string/app_name"
|
||||
android:supportsRtl="true">
|
||||
<service android:name="com.amap.api.location.APSService"></service>
|
||||
<meta-data android:name="com.amap.api.v2.apikey"
|
||||
android:value="c28ae1af5ce3c5f69254e21aea21affb">
|
||||
</meta-data>
|
||||
</application>
|
||||
</manifest>
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.fengliyan.location;
|
||||
|
||||
/**
|
||||
* Created by abby on 2018/4/7.
|
||||
*/
|
||||
|
||||
public class LocationBean {
|
||||
private String city;
|
||||
private String cityName;
|
||||
private String province;
|
||||
private String cityCode;
|
||||
private double longitude;
|
||||
private double latitude;
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getCityName() {
|
||||
return cityName;
|
||||
}
|
||||
|
||||
public void setCityName(String cityName) {
|
||||
this.cityName = cityName;
|
||||
}
|
||||
|
||||
public double getLongitude() {
|
||||
return longitude;
|
||||
}
|
||||
|
||||
public void setLongitude(double longitude) {
|
||||
this.longitude = longitude;
|
||||
}
|
||||
|
||||
public double getLatitude() {
|
||||
return latitude;
|
||||
}
|
||||
|
||||
public void setLatitude(double latitude) {
|
||||
this.latitude = latitude;
|
||||
}
|
||||
|
||||
public String getProvince() {
|
||||
return province;
|
||||
}
|
||||
|
||||
public void setProvince(String province) {
|
||||
this.province = province;
|
||||
}
|
||||
|
||||
public String getCityCode() {
|
||||
return cityCode;
|
||||
}
|
||||
|
||||
public void setCityCode(String cityCode) {
|
||||
this.cityCode = cityCode;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package com.fengliyan.location;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import com.amap.api.location.AMapLocation;
|
||||
import com.amap.api.location.AMapLocationClient;
|
||||
import com.amap.api.location.AMapLocationListener;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.ObservableEmitter;
|
||||
import io.reactivex.ObservableOnSubscribe;
|
||||
import io.reactivex.Observer;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
/**
|
||||
* Created by abby on 2018/3/22.
|
||||
*/
|
||||
|
||||
public class LocationManager {
|
||||
private static LocationManager mInstance;
|
||||
private AMapLocationClient mLocationClient;
|
||||
private List<ObservableEmitter<LocationBean>> mEmitterList = new ArrayList<>();
|
||||
private Observable<LocationBean> mObservable;
|
||||
|
||||
public static LocationManager getInstance() {
|
||||
if (null == mInstance) {
|
||||
mInstance = new LocationManager();
|
||||
}
|
||||
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
private LocationManager() {
|
||||
mObservable = Observable.create(new ObservableOnSubscribe<LocationBean>() {
|
||||
@Override
|
||||
public void subscribe(ObservableEmitter<LocationBean> emitter) throws Exception {
|
||||
mEmitterList.add(emitter);
|
||||
}
|
||||
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
|
||||
}
|
||||
|
||||
public void init(Context context) {
|
||||
try {
|
||||
mLocationClient = new AMapLocationClient(context);
|
||||
mLocationClient.setLocationListener(new AMapLocationListener() {
|
||||
@Override
|
||||
public void onLocationChanged(AMapLocation aMapLocation) {
|
||||
|
||||
Log.e("AmapError", "location Error, ErrCode:"
|
||||
+ aMapLocation.getErrorCode() + ", errInfo:"
|
||||
+ aMapLocation.getErrorInfo());
|
||||
|
||||
String city = aMapLocation.getCity().replace("市", "");
|
||||
String province = aMapLocation.getProvince();
|
||||
String cityCode = aMapLocation.getCityCode();
|
||||
double longitude = aMapLocation.getLongitude();
|
||||
double latitude = aMapLocation.getLatitude();
|
||||
LocationBean locationBean = new LocationBean();
|
||||
locationBean.setCity(city);
|
||||
locationBean.setCityName(aMapLocation.getCity());
|
||||
locationBean.setProvince(province);
|
||||
locationBean.setCityCode(cityCode);
|
||||
locationBean.setLatitude(latitude);
|
||||
locationBean.setLongitude(longitude);
|
||||
|
||||
|
||||
Iterator<ObservableEmitter<LocationBean>> i = mEmitterList.iterator();
|
||||
while (i.hasNext()) {
|
||||
ObservableEmitter<LocationBean> emitter = i.next();
|
||||
emitter.onNext(locationBean);
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void start() {
|
||||
if (null != mLocationClient) {
|
||||
mLocationClient.stopLocation();
|
||||
mLocationClient.startLocation();
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
if (null != mLocationClient) {
|
||||
mLocationClient.stopLocation();
|
||||
}
|
||||
}
|
||||
|
||||
public void addLocationObserver(Observer<LocationBean> observer) {
|
||||
mObservable.subscribe(observer);
|
||||
}
|
||||
|
||||
}
|
||||
3
location/src/main/res/values/strings.xml
Normal file
3
location/src/main/res/values/strings.xml
Normal file
@@ -0,0 +1,3 @@
|
||||
<resources>
|
||||
<string name="app_name">Location</string>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user