我想在android中創建應用程序,就像用戶將自己註冊到應用程序時一樣。管理員可以獲取所有註冊用戶的位置。我不知道該怎麼做。任何幫助將不勝感激。謝謝在android中獲取朋友的位置座標
0
A
回答
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
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>
而不是啓動服務
+0
如果工作,請標記答案權利 – Prashant
相關問題
- 1. Android - 如何獲取朋友的位置
- 2. 從Facebook獲取朋友的位置
- 3. 獲取Google Plus朋友Android
- 4. Android - 獲取GPS座標和位置
- 5. Android 4.4.2 - 獲取設備位置座標
- 6. 在iPhone中獲取Facebook/Twitter的朋友對Foursquare的位置。
- 7. 從座標獲取位置?
- 8. 獲取我朋友的朋友列表
- 9. 在Facebook應用程序中獲取朋友位置
- 10. SQL獲取朋友和朋友的朋友
- 11. 朋友的跟蹤位置
- 12. 在android中獲取當前位置(座標)?
- 13. Facebook的api android獲取朋友列表
- 14. Facebook FQL:找到位置在一定座標範圍內的朋友
- 15. 如何獲取用戶朋友中的每一個朋友的朋友數?
- 16. 從取座標獲取位置名稱
- 17. 獲取ajax請求的位置座標
- 18. 獲取Facebook朋友的位置是耗時的
- 19. 獲取的朋友的Facebook朋友在asp.net web應用程序
- 20. 如何獲取朋友的位置(經度和緯度)?
- 21. 通過Foursquare API獲取用戶朋友的位置
- 22. 使用Google Latitude API獲取朋友的位置
- 23. 獲取朋友的生日?
- 24. 獲取用戶的朋友
- 25. Facebook的SDK,獲取朋友
- 26. 如何獲取所有朋友的朋友,這是朋友在facebook的朋友列表
- 27. Sql獲取不是我的朋友的朋友的所有朋友
- 28. 圖 - 顯示朋友(和第一學位朋友)的朋友
- 29. dimple.js:獲取座標軸上的點/位置座標
- 30. 獲取用戶位置座標與MapKit
利用GPS傳感器的Android應用程序,以獲取緯度和經度座標...這是我的博客鏈接獲取工作代碼.... http://rahulraina09.blogspot.in/2014/03/android-gps-application-programming.html?m=1 –