package com.jasgroup.cn.amhdeam;

 import java.io.IOException;
import java.util.Iterator; import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast; import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.Layer;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISDynamicMapServiceLayer;
import com.esri.core.geometry.Envelope;
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 org.xmlpull.v1.XmlPullParserException; public class GpsActivity extends Activity {
private EditText editText;
private LocationManager lm;
private GraphicsLayer graphicsLayer = null;
private String latitude;
private String longitude;
private String ip = null;
private String port = null;
private String userid = null;
private MapView mMapView;
private LinearLayout linearLayout;
private Layer layer;
private static final String TAG = "GpsActivity"; @Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
// lm.removeUpdates(locationListener);
} @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.locationutil); editText = (EditText) findViewById(R.id.editText);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
SpatialReference S = SpatialReference.create(4326);
Envelope envelope = new Envelope(62.938320359601796, 37.37854569628149, 66.4928652149837, 39.6757739604943);
mMapView = new MapView(this, S, envelope); // mMapView = (MapView) findViewById(R.id.map); mMapView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
//添加地图
layer = new ArcGISDynamicMapServiceLayer("http://192.168.20.180:6080/arcgis/rest/services/gongquAB/MapServer");
mMapView.addLayer(layer);
mMapView.setExtent(envelope);
graphicsLayer = new GraphicsLayer();
mMapView.addLayer(graphicsLayer);
setContentView(mMapView);
/*
接受端口信息
*/
Bundle bundle = this.getIntent().getExtras();
if (bundle != null) {
userid = bundle.getString("userid");
ip = bundle.getString("ip");
port = bundle.getString("port");
}
// tv = (TextView) findViewById(R.id.tv);
// mMapView = (MapView) findViewById(R.id.map);
linearLayout = (LinearLayout) findViewById(R.id.llMap); //判断GPS是否正常启动
if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "请开启GPS导航...", Toast.LENGTH_SHORT).show();
//返回开启GPS导航设置界面
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 0);
return;
} //为获取地理位置信息时设置查询条件
String bestProvider = lm.getBestProvider(getCriteria(), true);
//获取位置信息
//如果不设置查询要求,getLastKnownLocation方法传人的参数为LocationManager.GPS_PROVIDER
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return;
}
Location location = lm.getLastKnownLocation(bestProvider);
updateToNewLocation(location);
//监听状态
lm.addGpsStatusListener(listener);
//绑定监听,有4个参数
//参数1,设备:有GPS_PROVIDER和NETWORK_PROVIDER两种
//参数2,位置信息更新周期,单位毫秒
//参数3,位置变化最小距离:当位置距离变化超过此值时,将更新位置信息
//参数4,监听
//备注:参数2和3,如果参数3不为0,则以参数3为准;参数3为0,则通过时间来定时更新;两者为0,则随时刷新 // 1秒更新一次,或最小位移变化超过1米更新一次;
//注意:此处更新准确度非常低,推荐在service里面启动一个Thread,在run中sleep(10000);然后执行handler.sendMessage(),更新位置
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
} @Override
protected void onResume() {
/**
* 设置为横屏
*/
if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return;
}
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1,
locationListener);
}
super.onResume();
}; //位置监听
private LocationListener locationListener = new LocationListener() { /**
* 位置信息变化时触发
*/
public void onLocationChanged(Location location) {
updateToNewLocation(location);
Log.i(TAG, "时间:" + location.getTime());
Log.i(TAG, "经度:" + location.getLongitude());
Log.i(TAG, "纬度:" + location.getLatitude());
Log.i(TAG, "海拔:" + location.getAltitude());
} /**
* GPS状态变化时触发
*/
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
//GPS状态为可见时
case LocationProvider.AVAILABLE:
Log.i(TAG, "当前GPS状态为可见状态");
break;
//GPS状态为服务区外时
case LocationProvider.OUT_OF_SERVICE:
Log.i(TAG, "当前GPS状态为服务区外状态");
break;
//GPS状态为暂停服务时
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Log.i(TAG, "当前GPS状态为暂停服务状态");
break;
}
} /**
* GPS开启时触发
*/
public void onProviderEnabled(String provider) {
if (ActivityCompat.checkSelfPermission(GpsActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(GpsActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Location location = lm.getLastKnownLocation(provider);
updateView(location);
} /**
* GPS禁用时触发
*/
public void onProviderDisabled(String provider) {
updateView(null);
} }; //状态监听
GpsStatus.Listener listener = new GpsStatus.Listener() {
public void onGpsStatusChanged(int event) {
switch (event) {
//第一次定位
case GpsStatus.GPS_EVENT_FIRST_FIX:
Log.i(TAG, "第一次定位");
break;
//卫星状态改变
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
Log.i(TAG, "卫星状态改变");
//获取当前状态
if (ActivityCompat.checkSelfPermission(GpsActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
GpsStatus gpsStatus = lm.getGpsStatus(null);
//获取卫星颗数的默认最大值
int maxSatellites = gpsStatus.getMaxSatellites();
//创建一个迭代器保存所有卫星
Iterator<GpsSatellite> iters = gpsStatus.getSatellites().iterator();
int count = 0;
while (iters.hasNext() && count <= maxSatellites) {
GpsSatellite s = iters.next();
count++;
}
System.out.println("搜索到:"+count+"颗卫星");
break;
//定位启动
case GpsStatus.GPS_EVENT_STARTED:
Log.i(TAG, "定位启动");
break;
//定位结束
case GpsStatus.GPS_EVENT_STOPPED:
Log.i(TAG, "定位结束");
break;
}
};
}; /**
* 实时更新文本内容
*
* @param location
*/
private void updateView(Location location){
if(location!=null){
editText.setText("设备位置信息\n\n经度:");
editText.append(String.valueOf(location.getLongitude()));
editText.append("\n纬度:");
editText.append(String.valueOf(location.getLatitude()));
}else{
//清空EditText对象
editText.getEditableText().clear();
}
} /**
* 返回查询条件
* @return
*/
private Criteria getCriteria(){
Criteria criteria=new Criteria();
//设置定位精确度 Criteria.ACCURACY_COARSE比较粗略,Criteria.ACCURACY_FINE则比较精细
criteria.setAccuracy(Criteria.ACCURACY_FINE);
//设置是否要求速度
criteria.setSpeedRequired(true);
// 设置是否允许运营商收费
criteria.setCostAllowed(false);
//设置是否需要方位信息
criteria.setBearingRequired(false);
//设置是否需要海拔信息
criteria.setAltitudeRequired(false);
// 设置对电源的需求
criteria.setPowerRequirement(Criteria.POWER_LOW);
return criteria;
}
private Location updateToNewLocation(Location location) {
System.out.println("--------zhixing--2--------");
String latLongString;
if (location != null) {
//设置当前坐标
Point point=new Point();
point.setX(location.getLongitude());
point.setY(location.getLatitude());
PictureMarkerSymbol pictureMarkerSymbol=new PictureMarkerSymbol(getResources().getDrawable(R.drawable.location1)); Graphic graphic=new Graphic(point,pictureMarkerSymbol);
// Graphic graphic=new Graphic(point,new SimpleMarkerSymbol(Color.RED,25, SimpleMarkerSymbol.STYLE.CIRCLE));
graphicsLayer.addGraphic(graphic);//添加地图中 latitude = Double.toString(location.getLatitude());
longitude = Double.toString(location.getLongitude());
latLongString = "纬度:" + latitude + "\n经度:" + longitude;
System.out.println("经度:" + longitude + "纬度:" + latitude);
sendp();
} else {
latLongString = "无法获取地理信息,请稍后...";
}
if(latitude!=null){
System.out.println("--------反馈信息----------"+ String.valueOf(latitude));
}
Toast.makeText(getApplicationContext(), latLongString, Toast.LENGTH_SHORT).show(); return location;
}
public void sendp(){
new Thread(){
@Override
public void run(){
// while (!exit){
try {
AsyncTaskUtil.getData(userid, latitude, longitude, ip, port); } catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
// }
}.start();
}
}

android GPS定位源码 地图显示位置源码 有用到的小伙伴自提取的更多相关文章

  1. Android GPS定位测试(附效果图)

    今天因为工作需要,把以前编写的一个GPS测试程序拿出来重新修改了一下.这个程序说起来有些历史了,是我11年编写的,那时候学了Android开发没多久,算是一个实验性的作品.现在工作需要,重新拿出来修整 ...

  2. Android GPS定位测试(附效果图)

    今天因为工作需要,把以前编写的一个GPS测试程序拿出来重新修改了一下.这个程序说起来有些历史了,是我11年编写的,那时候学了Android开发没多久,算是一个实验性的作品.现在工作需要,重新拿出来修整 ...

  3. Android GPS应用:动态获取位置信息

    在上文中,介绍了GPS概念及Android开发GPS应用涉及到的常用类和方法.在本文中,开发一个小应用,实时获取定位信息,包括用户所在的纬度.经度.高度.方向.移动速度等.代码如下: Activity ...

  4. android GPS 定位,取位置信息

    现在很多app ,需要取位置信息,所以我也做了一个模块用来取位置信息:   加入位置服务所需的权限: <uses-permission android:name="android.pe ...

  5. android gps定位LocationManager

    android location provider有: * LocationManager.GPS_PROVIDER:GPS,精度比较高,但是慢而且消耗电力,而且可能因为天气原因或者障碍物而无法获取卫 ...

  6. Android GPS定位 获取经纬度

    移动 是手机与手持设备的最大特点,可以通过Eclipse的DDMS视图,模拟设备的位置变化,改变经纬度后,点击send,然后运行程序,在应用程序中,动态的获取设备位置,然后显示当前的位置信息. 获取位 ...

  7. [置顶] xamarin android使用gps定位获取经纬度

    看了文章你会得出以下几个结论 1.android定位主要有四种方式GPS,Network(wifi定位.基站定位),AGPS定位 2.绝大部分android国产手机使用network进行定位是没有作用 ...

  8. 彻底解决Android GPS没法定位这一顽固问题

    大家去网上搜索Android定位location为null没法定位问题.预计有一大堆文章介绍怎样来解决.可是最后大家发现基本没用. 本文将从Android定位实现原理来深入分析没法定位原因并提出真正的 ...

  9. ArcGIS Runtime SDK for Android 定位权限(GPS定位\网络定位)

    ACCESS_COARSE_LOCATION和ACCESS_FINE_LOCATION: android.permission.ACCESS_COARSE_LOCATION:是基站定位,即基于无线网络 ...

随机推荐

  1. Python 基础之三条件判断与循环

    If……else 基本结构: If condition: do something else: do something 或者 If condition: do something elif cond ...

  2. Struts2入门(四)——数据输入验证

    一.前言 1.1.什么是输入验证?为什么需要输入验证? 在上一篇文章中,我们学习了数据类型转换,我们提到了表示层数据处理的两个方法,也提到了用户输入数据需要进行类型转换才能得到我们想要的数据,那么,我 ...

  3. input文本框录入字母自动大写

    向文本框输入文字时,如何让小写字母自动变为大写呢?有一个简单有效的做法是用CSS. <input name="t1" type="text" style= ...

  4. SVG颜色、渐变和填充

    颜色 RGB和HSL都是CSS3支持的颜色表示方法,一般普遍使用是RGB.PS:HSL浏览器兼容. RGB RGB即是代表红.绿.蓝三个通道的颜色,通过对红(R).绿(G).蓝(B)三个颜色通道的变化 ...

  5. JS函数声明的问题

    三个例子 var a = 10; 2 function test(){ 3 a = 100; 4 console.log(a); 5 console.log(this.a); 6 var a; 7 c ...

  6. ArcGIS Engine开发之量测功能

    1.距离测量 距离测量时,片段长度通过两点之间距离计算得到,全部长度通过片段长度的和计算得到.主要用到INewLineFeedback和IScreenDisplay两个接口. 1)INewLineFe ...

  7. iOS 语音朗读

    //判断版本大于7.0    if ([[[UIDevice currentDevice] systemVersion] integerValue] >= 7.0) {        NSStr ...

  8. 【转】用JitPack发布开源库时附加文档和源码

    来自:http://www.gcssloop.com/course/jitpack-sources-javadoc 用JitPack发布开源库时附加文档和源码 很早之前写过一篇用JitPack发布An ...

  9. 快速上手Unity原生Json库

    现在新版的Unity(印象中是从5.3开始)已经提供了原生的Json库,以前一直使用LitJson,研究了一下Unity用的JsonUtility工具类的使用,发现使用还挺方便的,所以打算把项目中的J ...

  10. grunt-笔记

    package.json: { "name": "grunt-uglify", "version": "1.0.0", ...