您的位置:首页 > 移动开发 > Android开发

android获取当前经纬度,并用地图显示跟踪

2015-06-02 15:44 519 查看
最近做了一个android的小东西(愁了一星期

):通过手机获取当前经纬度,通过线程(异步)时时通过socket向服务器发送定位数据,并调用google地图来追踪,并用textView来显示运行时的日志。关于怎样试用google地图这里就不啰嗦了,还用到了一些协议的解析和封装的,也就省略了

在配置文件中添加的权限:

1.<!--
声明链接换联网的权限 -->

<uses-permission android:name="android.permission.INTERNET" />

<!-- 声明获取GPS定为的权限 -->

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

2.在application中activity外添加
<uses-library android:name="com.google.android.maps" /> (google地图用到的,详细试用请百度)

第一部分Activity:

package com.unite;

import java.util.List;

import unite.xieyi.CongXieYi;

import unite.xieyi.XieYi;

import android.content.Context;

import android.content.Intent;

import android.graphics.drawable.Drawable;

import android.location.Location;

import android.location.LocationListener;

import android.location.LocationManager;

import android.net.ConnectivityManager;

import android.net.NetworkInfo;

import android.os.Bundle;

import android.os.Handler;

import android.os.HandlerThread;

import android.os.Looper;

import android.os.Message;

import android.provider.Settings;

import android.text.method.ScrollingMovementMethod;

import android.view.View;

import android.view.View.OnClickListener;

import android.webkit.WebView;

import android.webkit.WebViewClient;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ImageView;

import android.widget.TextView;

import android.widget.Toast;

import com.google.android.maps.GeoPoint;

import com.google.android.maps.MapActivity;

import com.google.android.maps.MapView;

import com.google.android.maps.Overlay;

import com.google.android.maps.OverlayItem;

import com.overlay.FirstOverlay;

public class UniteActivity extends MapActivity {

//声明控件

private
Button xianshi;

private
Button shezhi;

private
Button openGPS;

private
Button close;

private
Button map;

private
WebView view;

private
MapView mapView;

private
TextView logtext;

private
ImageView image;

MyHandler
myHandler;

String
Ip;//Ip

String
port;//端口号

String
fID ;//车场ID

String
sId ;//车载机ID

int lan ;//纬度

int
lon ;//经度

boolean
i = false;

int
hui = 0;

int
ce = 10;//10为没有GPS信号,11为有信号

public
void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.gps);

//获取传过来的值

Intent
it1 = getIntent();

fID
= it1.getStringExtra("FactoryId");

sId
= it1.getStringExtra("SimId");

//通过id获得控件对象

shezhi
= (Button) findViewById(R.id.shezhi);

xianshi
= (Button) findViewById(R.id.xianshi);

openGPS
= (Button) findViewById(R.id.openGPS);

close
= (Button) findViewById(R.id.close);

map
= (Button) findViewById(R.id.map);

view
= (WebView) findViewById(R.id.View);

image
= (ImageView) findViewById(R.id.image);

mapView
= (MapView) findViewById(R.id.mapview);//获得MapView对象

logtext
= (TextView) findViewById(R.id.logtext);

logtext.setMovementMethod(ScrollingMovementMethod.getInstance());//给textView添加滚动条

mapView.setBuiltInZoomControls(true);//设置mapview用于缩放的工具条

//将时间和BUtton控件绑定

xianshi.setOnClickListener(new
xianListener());

shezhi.setOnClickListener(new
sheListener());

openGPS.setOnClickListener(new
openGPSListener());

map.setOnClickListener(new
mapListener());

close.setOnClickListener(new
closeListener());

xianshi.setClickable(false);//按钮不可用

openGPS.setClickable(false);

close.setClickable(false);

map.setClickable(false);

if(fID!=null){

xianshi.setClickable(true);

openGPS.setClickable(true);

}

}

//关闭按钮事件处理

class
closeListener implements OnClickListener{

@Override

public
void onClick(View arg0) {

i
= true;

map.setClickable(false);

close.setClickable(false);

image.setVisibility(image.VISIBLE);

view.setVisibility(view.GONE);//改变显示状态为可见/不可见

mapView.setVisibility(mapView.GONE);

logtext.setText(logtext.getText().toString()+"\n"+"服务已关闭");

}

}

//地图按钮事件

class
mapListener implements OnClickListener{

@Override

public
void onClick(View arg0) {

if(lan
> 0 && lon > 0){

image.setVisibility(image.GONE);

view.setVisibility(view.GONE);//改变显示状态为可见/不可见

mapView.setVisibility(mapView.VISIBLE);

openMap();

}else{

logtext.setText(logtext.getText().toString()+"\n"+"地图暂时无法打开");

//Toast.makeText(UniteActivity.this,
"无法打开", Toast.LENGTH_SHORT).show();

}

}

}

//打开地图

private
void openMap(){

//mapView.setStreetView(true);//获取接到图片

//得到所有的图层对象

List<Overlay>
mapOverlays = mapView.getOverlays();

//清楚上一次的标记

mapOverlays.removeAll(mapOverlays);

//要显示的标记图片

Drawable
drawable = getResources().getDrawable(R.drawable.car_03);

//得到第一个标记对象

FirstOverlay
firstOverlay = new FirstOverlay(drawable, UniteActivity.this);

//通过经纬度指定地图的一个点

GeoPoint
point = new GeoPoint(lan,lon);

//当点击标记时显示的内容

OverlayItem
overlayItem = new OverlayItem(point, "当前坐标", "经度:"+lon+"纬度:"+lan);

//添加到标记中

firstOverlay.addOverlay(overlayItem);

mapOverlays.add(firstOverlay);

mapView.getController().setCenter(point);//设置地图中心

mapView.getController().setZoom(10);

}

//打开GPS按钮事件处理

class
openGPSListener implements OnClickListener{

@Override

public
void onClick(View arg0) {

//获取IP和端口号

String
Iport = XieYi.getCongXieYi();

Ip
= Iport.substring(0, 13);

port
= Iport.substring(13);

close.setClickable(true);

System.out.println("dddd:"+Iport);

if(Iport
!=null){

logtext.setText(logtext.getText().toString()+"\n"+"主服务已连接");

//Toast.makeText(UniteActivity.this,
"主服务已连接", Toast.LENGTH_SHORT).show();

}

openGPSSettings();

//生成HandlerThread对象

HandlerThread
handlerThread = new HandlerThread("handler_thread");

//启动,必须要有

handlerThread.start();

logtext.setText(logtext.getText().toString()+"\n"+"从服务已链接");

myHandler
= new MyHandler(handlerThread.getLooper());

myHandler.post(firstThread);

}

}

//显示按钮事件处理

class
xianListener implements OnClickListener{

@Override

public
void onClick(View v) {

if(fID
!= "" && sId != ""){

image.setVisibility(image.GONE);

view.setVisibility(view.VISIBLE);

mapView.setVisibility(mapView.GONE);

String
url = getIntent().getStringExtra("url");

//
view.setInitialScale(1);//这个方法直接缩到最小

view.loadUrl(url);

view.setWebViewClient(new
WebViewClientDemo());

logtext.setText(logtext.getText().toString()+"\n"+"远程车机已链接");

}else{

logtext.setText(logtext.getText().toString()+"\n"+"远程车机无法链接");

}

}

}

//将webview
作为客户端,可以嵌套其他控件

private
class WebViewClientDemo extends WebViewClient {

@Override

//
在WebView中而不是默认浏览器中显示页面

public
boolean shouldOverrideUrlLoading(WebView
view, String url) {

view.loadUrl(url);

return
true;

}

}

//设置按钮事件处理

class
sheListener implements OnClickListener{

@Override

public
void onClick(View v) {

Intent
it = new Intent();

it.putExtra("FactoryId",fID);

it.putExtra("SimId",
sId);

it.setClass(UniteActivity.this,
SheZhiActivity.class);

UniteActivity.this.startActivity(it);

}

}

//重写handler对象

class
MyHandler extends Handler{

public
MyHandler(){

}

public
MyHandler(Looper looper){

//继承自父类的looper

super(looper);

}

public
void handleMessage(Message msg){

try{

System.out.println("Thread
ID:"+Thread.currentThread().getId());

LocationManager
locationManager= (LocationManager) UniteActivity.this.getSystemService(Context.LOCATION_SERVICE);

//选择LocatonProvider
为GPS定为方式,绑定监听器

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0, 0,new LocationListner() );

//获取最近一次得到的经纬度

Location
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); // 通过GPS获取位置

if(ce
== 11){

if
(location != null) {

double
latitude = location.getLatitude();

double
longitude= location.getLongitude();

lan
= (int) (latitude*1E6);

lon
= (int) (longitude*1E6);

//调用链接从服务

int
d = CongXieYi.sendDingWei(Ip,port,longitude,latitude,sId,fID);

if(d
== 2){

map.setClickable(true);//将map按钮状态改成可用

//logtext.setText(logtext.getText().toString()+"\n"+"定位数据已发送");

}else{

//
logtext.setText(logtext.getText().toString()+"\n"+"从服务链接失败,请重试");

}

openMap();//打开地图

myHandler.post(firstThread);

}
else {

//logtext.setText(logtext.getText().toString()+"\n"+"无法获取经纬度,请重试");

myHandler.post(firstThread);

}

}

if(ce
== 10){

myHandler.post(firstThread);

}

}catch(Exception
e){

e.printStackTrace();

}

}

}

//实现线程

Runnable
firstThread = new Runnable() {

@Override

public
void run() {

if(i
== true){

i
= false;

return;

}else{

System.out.println("Runnable
Thread :" + Thread.currentThread().getId());

//通过Message对象传递参数

Message
msg = new Message();

myHandler.sendMessage(msg);

try
{

Thread.sleep(3000);

}
catch (InterruptedException e) {

//
TODO Auto-generated catch block

e.printStackTrace();

}

}

}

};

//判断用户是否开启GPS,如果没有提示让其开启

private
void openGPSSettings() {

LocationManager
alm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

if
(alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {

//Toast.makeText(this,
"GPS模块正常", Toast.LENGTH_SHORT).show();

logtext.setText(logtext.getText().toString()+"\n"+"GPS模块正常");

return;

}

//
Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();

Intent
intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);

startActivityForResult(intent,0);
//此为设置完成后返回到获取界面

}

//获得经纬度

private
class LocationListner implements LocationListener{

@Override

public
void onLocationChanged(Location location) {

if(location
==null){

System.out.println("asdasf");

myHandler.post(firstThread);

ce
= 10;

}else{

ce=11;

//当经纬度改变时调用

//Toast.makeText(UniteActivity.this,
"经度:"+location.getLongitude()+"纬度:"+location.getLatitude(), Toast.LENGTH_SHORT).show();

}

}

@Override

public
void onProviderDisabled(String provider) {

//
TODO Auto-generated method stub

}

@Override

public
void onProviderEnabled(String provider) {

//
TODO Auto-generated method stub

}

@Override

public
void onStatusChanged(String provider, int status, Bundle extras) {

//
TODO Auto-generated method stub

}

}

@Override

protected
boolean isRouteDisplayed() {

//
TODO Auto-generated method stub

return
false;

}

}

第三部分,xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<LinearLayout android:id="@+id/layout2"

android:layout_width="fill_parent"

android:layout_height="wrap_content">

<ImageView
android:id="@+id/image"

android:src="@drawable/unite"

android:layout_width="fill_parent"

android:layout_height="330dip"/>

<WebView
android:id="@+id/View"

android:layout_weight="1"

android:layout_width="fill_parent"

android:layout_height="330dip"

android:visibility="gone"

/>

<com.google.android.maps.MapView

android:id="@+id/mapview"

android:layout_width="fill_parent"

android:layout_weight="1"

android:layout_height="330dip"

android:enabled="true"

android:clickable="true"

android:visibility="gone"

android:apiKey="0uMk_6S4Pafz3Ez57nyxl2-0WWl_SGgwNOaVQKQ"

/>

</LinearLayout>

<LinearLayout android:id="@+id/layou3"

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginBottom="0dip"

>

<Button
android:id="@+id/xianshi"

android:layout_weight="1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:clickable="false"

android:textSize="10dip"

android:layout_margin="0dip"

android:text="显示"/>

<Button
android:id="@+id/openGPS"

android:layout_weight="1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="10dip"

android:layout_margin="0dip"

android:text="打开服务"/>

<Button
android:id="@+id/close"

android:layout_weight="1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="10dip"

android:layout_margin="0dip"

android:text="关闭服务"/>

<Button
android:id="@+id/map"

android:textSize="10dip"

android:layout_weight="1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="0dip"

android:text="地图"/>

<Button
android:id="@+id/shezhi"

android:layout_weight="1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="10dip"

android:layout_margin="0dip"

android:text="设置"/>

</LinearLayout>

<LinearLayout android:id="@+id/layou3"

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginBottom="0dip"

>

<TextView
android:id="@+id/logtext"

android:layout_width="fill_parent"

android:layout_height="50dip"

android:textSize="10dip"

android:scrollbars="vertical"

android:singleLine="false"

android:maxLines="100"

android:text="跟踪日志"/>

</LinearLayout>

</LinearLayout>

第四部分:显示效果





具体的流程:点击“设置”会跳转到一个页面(此时只有设置按钮可用),得到车场ID和车载机ID后返回该页面(此时"显示"和"打开服务"和"设置"按钮可用),点击"显示"按钮可以连接到远程车机网页,点击"打开服务"按钮可以开启线程通过socket向服务器发送定位信息了(此时"关闭服务"和"地图"也变成可用的),点击"地图"就可以打开google地图了,因为线程在执行所以当你的经纬度改变时在地图上也可以跟踪了,"关闭服务"关闭线程
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: