我想在Android中创建一个应用程序,就像用户将自己注册到应用程序一样。管理员可以获取已注册的所有用户的位置。我不知道该怎么做。任何帮助将不胜感激。感谢
答案 0 :(得分:0)
这个Android应用程序代码(使用gps传感器)可以帮助您获得纬度和经度(速度和距离可选)以及其他可以自行完成的事情(保存与主服务器协调)。
public class MainActivity extends Activity {
Location p1=new Location("point a");
Location p2=new Location("point b");
double longitude,latitude;
TextView speedview,xstart,ystart,distance,time;
float speed,dist;
LocationManager lm;
Button b;
int flag,flag1;
int hour,min,sec;
String location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
speedview=(TextView)findViewById(R.id.speedview);
xstart=(TextView)findViewById(R.id.slatitude);
b=(Button)findViewById(R.id.button1);
ystart=(TextView)findViewById(R.id.slongitude);
distance=(TextView)findViewById(R.id.distance);
time=(TextView)findViewById(R.id.time);
Toast.makeText(getApplicationContext(), "Wait while device check your position.", Toast.LENGTH_LONG).show();
flag = 0;
flag1 = 0;
hour = 0;
min = 0;
sec = 0;
lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
Log.d("Latitude","enable");
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
Log.d("Latitude","disable");
}
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
if(flag==0)
{
p1.setLatitude(arg0.getLatitude());
p1.setLongitude(arg0.getLongitude());
flag=1;
dist=(float)0.0;
}
xstart.setText("Latitude: "+String.valueOf(latitude));
ystart.setText("Longitude: "+String.valueOf(longitude));
latitude=arg0.getLatitude();
longitude=arg0.getLongitude();
speed=arg0.getSpeed();
speed=(float)((speed*10)/2.55);
speedview.setText(String.valueOf(speed)+" kmph");
p2.setLatitude(latitude);
p2.setLongitude(longitude);
dist=p2.distanceTo(p1);
if(flag1==1)
{
// distance.setText(String.valueOf(dist)+" km");
}
Date date=new Date();
}
});
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(flag1==0)
{
flag1=1;
flag=0;
b.setText("Stop");
//to start timer code
}
else
{
flag1=0;
b.setText("Start");
//to reset timer code
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
使用Android权限: -
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION
答案 1 :(得分:0)
首先制作服务
package com.tv.schlep2p.app.network;
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("Please allow access of location to get current location. To re-enable, please go to Settings and turn on Location Service for this app.");
// 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;
}
}
比制作活动添加此代码以获取位置
GPSTracker gps = new GPSTracker(getActivity());
if (gps.canGetLocation()) {
latitude = gps.getLatitude();
longitude = gps.getLongitude();
sLat = latitude;
sLong = longitude;
geocoder = new Geocoder(getActivity());
try {
addresses = geocoder.getFromLocation(latitude,
longitude, 1);
if (addresses.size() > 0) {
StringBuilder str = new StringBuilder();
if (Geocoder.isPresent()) {
Address returnAddress = addresses.get(0);
String more = returnAddress
.getFeatureName();
String more1 = returnAddress
.getAddressLine(1);
String more2 = returnAddress
.getAddressLine(2);
if (more1.equals(null)
&& more2.equals(null)) {
str.append(more);
} else if (more2.equals(null)) {
str.append(more + "," + more1);
} else {
str.append(more + "," + more1 + ","
+ more2);
}
_marker_def = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude,
longitude)));
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Constants
.showAlertDialog(
"Message",
"Unable to get location please try after some time",
getActivity(), false);
}
} else {
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
在您的maifest中添加此权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<service android:name="YOUR PASCKAGENAME.GPSTRACKER" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="YOUR PASCKAGENAME..LocationService" />
</intent-filter>
</service>
而不是开始你的服务