如何使用IP地址獲取當前位置?Android:如何定位應用程序?
0
A
回答
1
主題Obtaining user location結束於包括使用Android中的內置功能獲取位置。
報告還指出這一點:
網絡位置提供商提供了良好的位置數據,而無需使用GPS。
和:
限制一組提供商
取決於在使用你的應用環境或期望的精確程度,你可以選擇只使用網絡位置提供商或只有GPS,而不是兩者。只與其中一項服務進行交互,可能會降低電池使用的準確性。
0
爲什麼你想用ip地址找到位置? IP地址只能提供國家/地區詳細信息的位置信息,而且您依賴的是具有IP地理位置的Web服務,這種服務不太可靠,只有在您訪問互聯網時才能使用。另外,如果您要連接到應用程序中的服務器,那麼您可以讓服務器執行此操作,速度會更快。
你有設備上的GPS,可以根據經緯度給你實時的用戶位置。你可以按照鏈接在Joakim的回答
0
如果你想獲得移動位置,你需要第一個提供者在我的情況下,我使用谷歌API來從你的手機獲取GPS位置,你需要從API獲取令牌下面的教程解釋瞭如何從手機獲取位置真的很好。
http://blog.teamtreehouse.com/beginners-guide-location-android
並從android開發人員網絡,如果你雖然有疑慮。
http://developer.android.com/training/location/retrieve-current.html
http://developer.android.com/training/location/receive-location-updates.html
這是最重要的,我在開發我從一個片段得到的位置和我用的片段中的任何位置活動,其獲得的位置需要在我的Android應用程序的位置..
使用回調來請求移動電話的位置。
重要的,你需要的位置和其他工作的許可......
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>
XML的活動必須有片段:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="es.urjc.mov.javsan.fichas.LocationFragment"
android:id="@+id/location_fragment"
android:layout_height="0dip"
android:layout_weight="0"
android:layout_width="match_parent" />
</LinearLayout>
片段代碼:
package es.urjc.mov.javsan.fichas;
import android.Manifest;
import android.app.Fragment;
import android.content.IntentSender;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
public class LocationFragment extends Fragment implements
com.google.android.gms.location.LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = LocationFragment.class.getSimpleName();
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private android.location.Location currentLocation;
private View fragmentLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
fragmentLayout = inflater.inflate(R.layout.location_fragment, container, false);
buildLocation();
return fragmentLayout;
}
@Override
public void onStart() {
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
super.onStart();
}
@Override
public void onResume() {
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
super.onResume();
}
@Override
public void onStop() {
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
}
super.onStop();
}
@Override
public void onConnected(Bundle bundle) {
if (isDenyLocation()) {
requestAllowLocation();
return;
}
android.location.Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location != null) {
handleNewLocation(location);
} else {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
}
@Override
public void onConnectionSuspended(int i) {
Log.e(TAG, String.format("%s\n", "Connection suspended please reconnect..."));
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.v(TAG, String.format("Connection failed : %s", connectionResult.toString()));
if (!connectionResult.hasResolution()) {
Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode());
return;
}
try {
// Start an Activity that tries to resolve the error
connectionResult.startResolutionForResult(getActivity(), CONNECTION_FAILURE_RESOLUTION_REQUEST);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
@Override
public void onLocationChanged(Location location) {
handleNewLocation(location);
}
private void buildLocation() {
if (mGoogleApiClient == null) {
// Create the GoogleApiClient Object.
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.addApi(AppIndex.API).build();
}
if (mLocationRequest == null) {
// Create the LocationRequest Object.
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000) // 10 seconds, in milliseconds
.setFastestInterval(1 * 1000); // 1 second, in milliseconds
}
}
private void handleNewLocation(Location location) {
currentLocation = location;
Log.e(TAG, String.format("Current Location : %f, %f",
currentLocation.getLongitude(), currentLocation.getLatitude()));
}
我希望這個幫助!
Javi,
相關問題
- 1. Android位置應用程序
- 2. Android「應用程序鎖定」應用程序如何工作?
- 3. iPad應用程序定位
- 4. 如何找到Android應用程序是32位或64位
- 5. Android鎖定應用程序
- 6. 如何使用Zxing應用程序掃描android中的位圖?
- 7. 在Android上的位置應用程序
- 8. android-位置應用程序優化
- 9. 基於位置的Android應用程序
- 10. 如何用線程構建Android定時器應用程序?
- 11. 如何防止應用程序竊取(特定於Android應用程序)?
- 12. 如何在android中製作應用程序鎖定應用程序?
- 13. 如何確定應用程序抽屜中的Android應用程序列表
- 14. 如何用Android應用程序打開Facebook應用程序?
- 15. 使用wifi信號的室內定位android應用程序
- 16. iPhone/iPad通用應用程序定位
- 17. 如何在android應用程序中獲取當前位置
- 18. 如何獲取Android應用程序中的位置項?
- 19. 如何發送Android應用程序的位置到服務器?
- 20. 如何從android應用程序小部件獲取GPS位置?
- 21. 如何從Java Web應用程序啓動Android應用程序?
- 22. Android:應用程序Protector應用程序如何工作?
- 23. 如何將AngularJS應用程序轉換爲android應用程序
- 24. 如何將Android應用程序拆分爲lib +應用程序
- 25. 如何將j2me應用程序轉換爲android應用程序?
- 26. 如何獲得Android應用程序的應用程序列表?
- 27. Android應用程序和Web應用程序如何工作?
- 28. 如何使用KeyEvent定位正在運行的應用程序?
- 29. 你如何在Windows通用應用程序內定位
- 30. 如何使Android應用程序,確定位置,並找到最近
你應該用Android標記這個問題,這樣,只有想要回答Android問題的人才會發現它更容易。 – 2010-12-09 13:37:45