Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取用户位置:Android_Java_Android_Xml_Android Layout_Location - Fatal编程技术网

Java 获取用户位置:Android

Java 获取用户位置:Android,java,android,xml,android-layout,location,Java,Android,Xml,Android Layout,Location,所以我在互联网上搜索了很多不同的解决方案,让我的android手机通过谷歌地图API使用play服务显示当前位置。我相信我有正确的设置,但有些地方不对劲 请参见下面的.java和.xml代码 爪哇 XML 非常感谢尝试使用此类获取纬度和经度 import android.app.AlertDialog.Builder; import android.app.Service; import android.content.Context; import android.content.Dial

所以我在互联网上搜索了很多不同的解决方案,让我的android手机通过谷歌地图API使用play服务显示当前位置。我相信我有正确的设置,但有些地方不对劲

请参见下面的.java和.xml代码

爪哇

XML



非常感谢

尝试使用此类获取纬度和经度

import android.app.AlertDialog.Builder;
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;

public class GPSTracker extends Service implements LocationListener {


private Context context;

boolean isGPSEnabled=false;
boolean isNetworkEnabled=false;
boolean canGetLocation=false;

Location location;

double latitude;
double longitude;

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES=10;
private static final long MIN_TIME_BW_UPDATES=1000*60*1;

protected LocationManager locationManager;

public GPSTracker(Context context)
{
    this.context=context;
getLocation();
}

public Location getLocation()
{
    try{
    locationManager=(LocationManager) context.getSystemService(LOCATION_SERVICE);
    isGPSEnabled=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    isNetworkEnabled=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);




    if(!isGPSEnabled && !isNetworkEnabled)
    {
        showSettingsAlert();
    }
    else{
        this.canGetLocation=true;
        if(isNetworkEnabled)
        {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);


        if(locationManager !=null)
        {
            location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if(location !=null)
        {
            latitude=location.getLatitude();
            longitude=location.getLongitude();
        }
        }
        }

        if(isGPSEnabled){
            if(location==null)
            {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                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;
}

public void stopUsingGPS()
{
    if(locationManager !=null)
    {
        locationManager.removeUpdates(GPSTracker.this);
    }
}

public double getLatitude()
{
    if(location!=null)
    {
        latitude=location.getLatitude();
    }
    return latitude;

}


public double getLongitude()
{
    if(location!=null)
    {
        longitude=location.getLongitude();
    }
    return longitude;

}

public boolean canGetLocation(){

return this.canGetLocation;

}

public void showSettingsAlert(){
    Builder alertDialog=new Builder(context);
    alertDialog.setTitle("GPS Settings");
    alertDialog.setMessage("GPS is not enabled.Do you want to go to the settings menu?");
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent intent=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            context.startActivity(intent);
        }
    }); 

    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();

        }
    });
    alertDialog.show();
}


@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}


}
在地图上标出你的位置

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

 public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
GPSTracker gps;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}



@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;



    try {

        gps = new GPSTracker(MapsActivity.this);

        if (gps.canGetLocation) {
            double latitude = gps.getLatitude();
            double longitude = gps.getLongitude();
            LatLng sydney = new LatLng(latitude, longitude);
            mMap.addMarker(new MarkerOptions().position(sydney).title("I am here"));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
            mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

            for(int i=0;i<MainActivity.nameArray.length;i++)
            {
                latitude=Double.parseDouble(MainActivity.latArray[i]);
                longitude=Double.parseDouble(MainActivity.lonArray[i]);
                LatLng shops = new LatLng(latitude, longitude);
                mMap.addMarker(new MarkerOptions().position(shops).title(MainActivity.nameArray[i]));

            }





        } else {
            gps.showSettingsAlert();
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}
导入android.os.Bundle;
导入android.support.v4.app.FragmentActivity;
导入com.google.android.gms.maps.CameraUpdateFactory;
导入com.google.android.gms.maps.GoogleMap;
导入com.google.android.gms.maps.OnMapReadyCallback;
导入com.google.android.gms.maps.SupportMapFragment;
导入com.google.android.gms.maps.model.LatLng;
导入com.google.android.gms.maps.model.MarkerOptions;
公共类MapsActivity扩展了FragmentActivity在MapReadyCallback上的实现{
私有谷歌地图;
全球定位系统;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
试一试{
gps=新的GP斯特拉克(MapsActivity.this);
if(gps.canGetLocation){
双纬度=gps.getLatitude();
double longitude=gps.getLongitude();
LatLng sydney=新LatLng(纬度、经度);
mMap.addMarker(newmarkeroptions().position(sydney.title)(“我在这里”);
mMap.moveCamera(CameraUpdateFactory.newLatLng(悉尼));
mMap.animateCamera(CameraUpdateFactory.zoomTo(10),2000,空);

对于(int i=0;这有很多潜在的问题。首先,你需要确保你的清单中有访问位置的权限。其次,如果你在Android API 23上开发,你必须实现检查自我权限块,这样你才能获得访问位置的权限。还要注意,如果位置是ut null,在声明“双纬度”和“双经度”时会出现异常。此外,不推荐使用GoogleMap.getMyLocation(),请参阅
import android.app.AlertDialog.Builder;
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;

public class GPSTracker extends Service implements LocationListener {


private Context context;

boolean isGPSEnabled=false;
boolean isNetworkEnabled=false;
boolean canGetLocation=false;

Location location;

double latitude;
double longitude;

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES=10;
private static final long MIN_TIME_BW_UPDATES=1000*60*1;

protected LocationManager locationManager;

public GPSTracker(Context context)
{
    this.context=context;
getLocation();
}

public Location getLocation()
{
    try{
    locationManager=(LocationManager) context.getSystemService(LOCATION_SERVICE);
    isGPSEnabled=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    isNetworkEnabled=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);




    if(!isGPSEnabled && !isNetworkEnabled)
    {
        showSettingsAlert();
    }
    else{
        this.canGetLocation=true;
        if(isNetworkEnabled)
        {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);


        if(locationManager !=null)
        {
            location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if(location !=null)
        {
            latitude=location.getLatitude();
            longitude=location.getLongitude();
        }
        }
        }

        if(isGPSEnabled){
            if(location==null)
            {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                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;
}

public void stopUsingGPS()
{
    if(locationManager !=null)
    {
        locationManager.removeUpdates(GPSTracker.this);
    }
}

public double getLatitude()
{
    if(location!=null)
    {
        latitude=location.getLatitude();
    }
    return latitude;

}


public double getLongitude()
{
    if(location!=null)
    {
        longitude=location.getLongitude();
    }
    return longitude;

}

public boolean canGetLocation(){

return this.canGetLocation;

}

public void showSettingsAlert(){
    Builder alertDialog=new Builder(context);
    alertDialog.setTitle("GPS Settings");
    alertDialog.setMessage("GPS is not enabled.Do you want to go to the settings menu?");
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent intent=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            context.startActivity(intent);
        }
    }); 

    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();

        }
    });
    alertDialog.show();
}


@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}


}
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

 public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
GPSTracker gps;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}



@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;



    try {

        gps = new GPSTracker(MapsActivity.this);

        if (gps.canGetLocation) {
            double latitude = gps.getLatitude();
            double longitude = gps.getLongitude();
            LatLng sydney = new LatLng(latitude, longitude);
            mMap.addMarker(new MarkerOptions().position(sydney).title("I am here"));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
            mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

            for(int i=0;i<MainActivity.nameArray.length;i++)
            {
                latitude=Double.parseDouble(MainActivity.latArray[i]);
                longitude=Double.parseDouble(MainActivity.lonArray[i]);
                LatLng shops = new LatLng(latitude, longitude);
                mMap.addMarker(new MarkerOptions().position(shops).title(MainActivity.nameArray[i]));

            }





        } else {
            gps.showSettingsAlert();
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}