如何调用事件depening在latitide和经度? [英] How to call event depening upon latitide and longitude?
问题描述
我正在开发一个位置基本应用程序。其确定的经度和纬度。
现在我想将它与相同的值进行比较,如果匹配我想打电话给事件。
我尝试它在许多方面,但无法做到这一点。请给一个想法,code PLZ。
工作我做直到现在
主要活动
包com.example.gpstracking;进口android.app.Activity;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.Toast;公共类AndroidGPSTrackingActivity延伸活动{按钮btnShowLocation;// GPSTracker类
GPSTracker GPS; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main); btnShowLocation =(按钮)findViewById(R.id.btnShowLocation); //展示位置按钮单击事件
btnShowLocation.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(查看为arg0){
//创建类对象
GPS =新GPSTracker(AndroidGPSTrackingActivity.this); //检查是否启用GPS
如果(gps.canGetLocation()){ 双纬度= gps.getLatitude();
双经度= gps.getLongitude(); // \\ n是新行
Toast.makeText(getApplicationContext(),您现在的位置 - \\ nLat:+纬度+\\ nLong:+经度,Toast.LENGTH_LONG).show();
}其他{
//无法获取位置
未启用// GPS或网络
//要求用户在设置中启用GPS /网络
gps.showSettingsAlert(); } }
});
} }
和子活动
包com.example.gpstracking;进口android.app.AlertDialog;
进口android.app.Service;
进口android.content.Context;
进口android.content.DialogInterface;
进口android.content.Intent;
进口android.location.Location;
进口android.location.LocationListener;
进口android.location.LocationManager;
进口android.os.Bundle;
进口android.os.IBinder;
进口android.provider.Settings;
进口android.util.Log;公共类GPSTracker扩展服务实现LocationListener的{私人最终上下文mContext;//标志GPS状态
布尔isGPSEnabled = FALSE;//标志网络状态
布尔isNetworkEnabled = FALSE;//标志GPS状态
布尔canGetLocation = FALSE;地点位置; // 位置
双纬度; //纬度
双经度; //经度//最小距离改变米的更新
私有静态最后长MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; //10米//毫秒更新之间的最短时间
私有静态最后长MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1分钟//声明一个位置管理
受保护的LocationManager的LocationManager;公共GPSTracker(上下文的背景下){
this.mContext =背景;
的getLocation();
}公共场所的getLocation(){
尝试{
的LocationManager =(的LocationManager)mContext
.getSystemService(LOCATION_SERVICE); //获取GPS状态
isGPSEnabled =的LocationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER); //获取网络状态
isNetworkEnabled =的LocationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 如果(isGPSEnabled&安培;!&安培;!isNetworkEnabled){
//没有启用网络供应商
}其他{
this.canGetLocation = TRUE;
如果(isNetworkEnabled){
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES,这一点);
Log.d(网络,网络);
如果(的LocationManager!= NULL){
位置=的LocationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
如果(位置!= NULL){
纬度= location.getLatitude();
经度= location.getLongitude();
}
}
}
//如果GPS功能的获得纬度/长期使用GPS服务
如果(isGPSEnabled){
如果(位置== NULL){
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES,这一点);
Log.d(GPS已启用,GPS功能的);
如果(的LocationManager!= NULL){
位置=的LocationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
如果(位置!= NULL){
纬度= location.getLatitude();
经度= location.getLongitude();
}
}
}
}
} }赶上(例外五){
e.printStackTrace();
} 返回的位置;
}/ **
*停止使用GPS监听器
*调用此函数将停止在你的应用程序中使用GPS
* * /
公共无效stopUsingGPS(){
如果(的LocationManager!= NULL){
locationManager.removeUpdates(GPSTracker.this);
}
}/ **
*函数来获取纬度
* * /
公共双getLatitude(){
如果(位置!= NULL){
纬度= location.getLatitude();
} //返回纬度
返回纬度;
}/ **
*函数来获取经度
* * /
公共双getLongitude(){
如果(位置!= NULL){
经度= location.getLongitude();
} //返回经度
返回经度;
}/ **
*功能检查启用GPS / WIFI
* @返回布尔
* * /
公共布尔canGetLocation(){
返回this.canGetLocation;
}/ **
*功能,以显示设置警告对话框
*在pressing设置按钮将劳克设置选项
* * /
公共无效showSettingsAlert(){
AlertDialog.Builder alertDialog =新AlertDialog.Builder(mContext); //设置对话框标题
alertDialog.setTitle(GPS是设置); //设置对话框留言
alertDialog.setMessage(GPS未启用你想要去设置菜单?); //在pressing设置按钮
alertDialog.setPositiveButton(设置,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释它){
意向意图=新意图(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(意向);
}
}); //在pressing取消按钮
alertDialog.setNegativeButton(取消,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释它){
dialog.cancel();
}
}); //显示警报信息
alertDialog.show();
}@覆盖
公共无效onLocationChanged(地点){
}@覆盖
公共无效onProviderDisabled(字符串提供商){
}@覆盖
公共无效onProviderEnabled(字符串提供商){
}@覆盖
公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){
}@覆盖
公众的IBinder onBind(意向为arg0){
返回null;
} }
使用的onLocationChanged() - 方法已在您的code。如果用户移动,你可以做你的坐标进行比较,然后激发您的活动。
http://developer.android.com/reference/android/location/ LocationListener.html
更新:这是code,你可以用它来检测是否达到了一定的目的:
请为latitute和经度在你的类两个变量。因此,例如:
双纬度= 10.0;
双LON = 5.0;
然后修改onLocationChanged() - 方法是这样的:
@覆盖
公共无效onLocationChanged(地点){
双currentLat = location.getLatitude();
双currentLon = location.getLongitude();
//你将需要创建或重用的距离功能......这应该是很容易容易找到...
如果(距离(纬度,经度,currentLat,currentLon)LT; 2.0){
//你想做的事...
}
}
因此,对于每个位置的变化,该手机检测,这将在距离比较到上次的距离和触发事件...
i am developing a location base application. which determines the latitude and longitude.
now i want to compare it with same values and if matched i want to call event. i try it in many ways but unable to do it. kindly give an idea and code plz. work that i did till now
Main activity
package com.example.gpstracking;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class AndroidGPSTrackingActivity extends Activity {
Button btnShowLocation;
// GPSTracker class
GPSTracker gps;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
// show location button click event
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// create class object
gps = new GPSTracker(AndroidGPSTrackingActivity.this);
// check if GPS enabled
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
// \n is for new line
Toast.makeText(getApplicationContext(), "Your Location - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
}else{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
}
});
}
}
and sub activity
package com.example.gpstracking;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
// Declaring a Location Manager
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
* */
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GPSTracker.this);
}
}
/**
* Function to get latitude
* */
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
/**
* Function to check GPS/wifi enabled
* @return boolean
* */
public boolean canGetLocation() {
return this.canGetLocation;
}
/**
* Function to show settings alert dialog
* On pressing Settings button will lauch Settings Options
* */
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
Use the onLocationChanged()-method that is already in your code. If the user moves, you can do your coordinate-comparison and then fire your event.
http://developer.android.com/reference/android/location/LocationListener.html
Update: This is the code that you could use to detect whether you reached a certain destination:
Make two variables for latitute and longitude in your class. So for example:
double lat=10.0;
double lon=5.0;
Then modify the onLocationChanged()-Method to something like this:
@Override
public void onLocationChanged(Location location) {
double currentLat=location.getLatitude();
double currentLon=location.getLongitude();
//you will need to create or reuse a distance-function... this should be easily findable...
if (distance(lat,lon,currentLat,currentLon)<2.0){
//do what you want to do...
}
}
So for every location change that the phone detects, it will compare the distance to the last distance and fire the event...
这篇关于如何调用事件depening在latitide和经度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!