android gps 锁屏更新坐标_Andriod 获得GPS经纬度

package com.example.yanlei.ylmap;import android.app.Activity;import android.app.Application;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.net.Uri;import android.os.Bundle;import android.os.Handler;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.widget.TextView;import com.esri.android.map.Callout;import com.esri.android.map.GraphicsLayer;import com.esri.android.map.MapView;import com.esri.core.geometry.GeometryEngine;import com.esri.core.geometry.Point;import com.esri.core.geometry.SpatialReference;import com.esri.core.map.Graphic;import com.esri.core.symbol.PictureMarkerSymbol;import java.math.BigDecimal;/** * Created by yanlei on 2017-02-07. */public class GetGPS {private LocationManager locationManager;//位置    private GraphicsLayer graphicsLayer;private MapView mapView = null;private String TAG;
TextView pTextView;private double latitude = 0.0;private double longitude = 0.0;private Callout callout = null;private Context pContext;private void delay() {
toggleGPS();new Handler() {
}.postDelayed(new Runnable() {
@Overridepublic void run() {
getLocation();
}
}, 2000);
}public void getCoord(
LocationManager plocationManager, GraphicsLayer pgraphicsLayer,
MapView pmapView, String pTAG, TextView p_TextView, Context p_Context
) {
Log.d(TAG, "这里");this.locationManager = plocationManager;this.graphicsLayer = pgraphicsLayer;this.mapView = pmapView;this.TAG = pTAG;this.pTextView = p_TextView;this.pContext = p_Context;
Log.d(TAG, "这里1=====");if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.d(TAG, "这里1111111111111111111");
getLocation();//gps已打开 } else {
delay();
}
}//投影坐标转地理坐标 public static Point ProjectToGCS(double X, double Y, MapView mapView) {
Point wgspoint = new Point(X, Y);
Point mapPoint = (Point) GeometryEngine.project(wgspoint,
mapView.getSpatialReference(),//GCS_Beijing_1954 //输入坐标系 SpatialReference.create(4214));//输出坐标系 return mapPoint;
}//地理坐标转投影坐标 public static Point GCSToProject(double X, double Y, MapView mapView) {
Point wgspoint = new Point(X, Y);
Point mapPoint = (Point) GeometryEngine.project(wgspoint,
SpatialReference.create(4214),//GCS_Beijing_1954 mapView.getSpatialReference());return mapPoint;
}public void ShowPointOnMap(Point mapPoint) {//清空定位图层 graphicsLayer.removeAll();//接收到的GPS的信号X(lat),Y(lon) YLPub.showToast("X=" + mapPoint.getX() + ",Y=" + mapPoint.getY());//图层的创建// Graphic graphic = new Graphic(mapPoint,new SimpleMarkerSymbol(Color.RED,18,STYLE.CIRCLE)); Log.d(TAG, "ShowPointOnMap: ");
PictureMarkerSymbol pms = new PictureMarkerSymbol(pContext.getResources().getDrawable(
R.drawable.location));
Log.d(TAG, "Show:=======================");
Graphic graphic = new Graphic(mapPoint, pms);graphicsLayer.addGraphic(graphic);
}//保留2位小数 public static double get2num(double scale) {
BigDecimal bigDecimal = new BigDecimal(scale);
scale = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();return scale;
}//度转换为度分秒 public static String trandu2m(double d) { //gisoracle 编号 try {//double dd = Convert.ToDouble(str); String str = "" + d;int p = str.indexOf(".");int dt = Integer.parseInt(str.substring(0, p));
d = d - dt;double M = d * 60;int mt = (int) M;
M = (M - mt) * 60;if (Math.abs(M - 60) < 0.001) {
M = 0;
mt = mt + 1;
}if (mt == 60) {
dt = dt + 1;
mt = 0;
}return "" + dt + "°" + mt + "′" + get2num(M) + "″";
} catch (Exception e) {return e.getMessage();
}
}private void getLocation() {
Log.d(TAG, "getLocation: ");
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);if (location != null) {latitude = location.getLatitude();longitude = location.getLongitude();
} else {locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
}
Point pt = GCSToProject(longitude, latitude, mapView);mapView.centerAt(pt, true);
ShowPointOnMap(pt);//mapView.refreshDrawableState(); String mystr = "纬度:" + trandu2m(latitude) + "\n" + "经度:" + trandu2m(longitude);pTextView.setText(mystr);callout = mapView.getCallout();
LayoutInflater inflater = LayoutInflater.from(pContext);
View view = inflater.inflate(R.layout.callout_view, null);
((TextView) view.findViewById(R.id.textView1)).setText(mystr);
View closebtn = view.findViewById(R.id.btnclose);//定位 closebtn.setOnClickListener(new CloseClickListener());callout.show(pt, view);
}private class CloseClickListener implements View.OnClickListener {public void onClick(View v) {if (callout != null) {callout.hide();
}
}
}
LocationListener locationListener = new LocationListener() {// Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数 @Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {
}// Provider被enable时触发此函数,比如GPS被打开 @Overridepublic void onProviderEnabled(String provider) {
Log.e(TAG, provider);
}// Provider被disable时触发此函数,比如GPS被关闭 @Overridepublic void onProviderDisabled(String provider) {
Log.e(TAG, provider);
}// 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发 @Overridepublic void onLocationChanged(Location location) {if (location != null) {
Log.e("Map", "Location changed : Lat: " + location.getLatitude() + " Lng: " + location.getLongitude());latitude = location.getLatitude(); // 经度 longitude = location.getLongitude(); // 纬度 }
}
};private void toggleGPS() {
Intent gpsIntent = new Intent();
gpsIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
gpsIntent.setData(Uri.parse("custom:3"));try {
PendingIntent.getBroadcast(pContext, 0, gpsIntent, 0).send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
Location location1 = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);if (location1 != null) {latitude = location1.getLatitude(); // 经度 longitude = location1.getLongitude(); // 纬度 }
}
}
}

权限

xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.yanlei.ylmap">
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_MMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.NFC"/>
<uses-permission android:name="android.permission.TRANSMIT_IR"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.RECORD_AUDIO"/> <uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />intent-filter>activity>application>manifest>

ffaa34f409a39571c3b7027f7fc1fdb9.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值