使用GPS位置映射活动

时间:2017-12-30 23:29:15

标签: java android gps maps

我的地图活动功能齐全,我已在清单中启用了gps精确位置权限。我怎样才能从我的gps纬度和经度显示地图活动?

代码如下所示,不起作用。运行应用后,地图无法显示,只有Google徽标才会显示。删除位置代码,它可以正常工作。

 package com.example.alijammal.maps;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;

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;
private double longitude;
private double latitude;

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



final int requestcode=128;
String locationprovider = LocationManager.GPS_PROVIDER;

LocationManager locationManager; //start or stop request location update
LocationListener locationListener; //;listen for changes




protected void onResume() {
    super.onResume();
    Log.d("testing", "ONRESUME CALLED");
    Log.d("testing", "GETTING WEATHER OF CURRENT" +
            "LOCATION");
    getWeatherLocation();


}

private void getWeatherLocation() {
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Log.d("testing", "on location change");
             longitude = location.getLongitude();
             latitude = location.getLatitude();
            Log.d("testing","Longitude is "+longitude);
            Log.d("testing","Latitude is "+latitude);

        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {
            //if wifi or gps is disabled on phone

            Log.d("testing", "onproviderdiable");
        }
    };
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},requestcode);
        return;
    }
    locationManager.requestLocationUpdates(locationprovider, 5000, 1000, locationListener);

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(requestCode==requestcode){
        if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED)
            Log.d("testing","granted");
        getWeatherLocation();
    }else
    {
        Log.d("testing","denied");


    }
}


/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng loca = new LatLng(longitude, latitude);
    mMap.addMarker(new MarkerOptions().position(loca).title("Marked"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(loca));
  } 
}

0 个答案:

没有答案