Android:如何定位应用程序?

Android:如何定位应用程序?,android,geolocation,ip-address,Android,Geolocation,Ip Address,如何使用IP地址获取当前位置?主题位于使用Android内置功能获取位置的封面 它还指出: 网络位置提供商提供良好的位置数据,无需使用GPS 以及: 限制一组提供程序 根据应用程序的使用环境或所需的精度级别,您可以选择仅使用网络位置提供程序或仅使用GPS,而不是同时使用两者。仅与其中一项服务交互可减少电池使用,但可能会降低准确性 为什么要使用ip地址查找位置?IP地址只能提供包含国家/地区详细信息的位置信息,而且您还依赖于使用IP进行地理位置定位的Web服务。IP地址不太可靠,只有在您可以访问i

如何使用IP地址获取当前位置?

主题位于使用Android内置功能获取位置的封面

它还指出:

网络位置提供商提供良好的位置数据,无需使用GPS

以及:

限制一组提供程序

根据应用程序的使用环境或所需的精度级别,您可以选择仅使用网络位置提供程序或仅使用GPS,而不是同时使用两者。仅与其中一项服务交互可减少电池使用,但可能会降低准确性


为什么要使用ip地址查找位置?IP地址只能提供包含国家/地区详细信息的位置信息,而且您还依赖于使用IP进行地理位置定位的Web服务。IP地址不太可靠,只有在您可以访问internet时才能工作。此外,如果您正在连接应用程序中的服务器,那么您可以让服务器执行此操作,这将快得多


您的设备上有GPS,它将根据lat和lon为您提供实时用户位置。你可以按照Joakim答案中的链接进行操作

如果你想获得移动位置,你首先需要一个提供商在我的情况下,我使用谷歌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应用程序中获取位置

使用回调请求移动电话的位置

http://blog.teamtreehouse.com/beginners-guide-location-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"/>
我希望这有帮助


Javi,

你应该在这个问题上加上安卓系统的标签,这样,只想回答安卓系统问题的人会觉得更容易。
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()));
}