位置显示在地图中

时间:2017-10-01 11:41:03

标签: android dictionary

嘿伙计我只是想让一个应用程序在谷歌地图上显示我的位置。我尝试运行此代码以显示我在谷歌地图中的当前位置但它不显示地图上的任何内容(它显示地图但不是那个地点) 该应用程序是runnng但位置标记不存在。请帮助

 public class MapsActivity extends FragmentActivity implements 
 OnMapReadyCallback {
 LatLng  myPosition;
private GoogleMap mMap;
private FusedLocationProviderClient mFusedLocationClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    SupportMapFragment mapFragment = (SupportMapFragment) 

   getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

}




@Override
public void onMapReady(final GoogleMap googleMap) {
    mMap = googleMap;
    mFusedLocationClient = 
 LocationServices.getFusedLocationProviderClient(this);



    if (ActivityCompat.checkSelfPermission(this, 
 android.Manifest.permission.ACCESS_FINE_LOCATION) != 
 PackageManager.PERMISSION_GRANTED && 
 ActivityCompat.checkSelfPermission(this, 
 android.Manifest.permission.ACCESS_COARSE_LOCATION) != 
 PackageManager.PERMISSION_GRANTED) {

        return;
    }
    Task task= mFusedLocationClient.getLastLocation()
            .addOnSuccessListener(this, new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {


                    Toast.makeText(getBaseContext(),"code run here",
                            Toast.LENGTH_SHORT).show();
                    if (location != null) {
                        double latitude = location.getLatitude();
                        double longitude = location.getLongitude();
                        String a,b;
                        a= String.valueOf(latitude);
                        b= String.valueOf(longitude);

                        Log.d(a,b);
                        Toast.makeText(getBaseContext(),a+b,
                                Toast.LENGTH_SHORT).show();
                        LatLng latLng = new LatLng(latitude, longitude);
                        myPosition = new LatLng(latitude, longitude);
                        googleMap.addMarker(new 
 MarkerOptions().position(myPosition).title("Start"));

 mMap.moveCamera(CameraUpdateFactory.newLatLng(myPosition));
                    }
                }
            });
}
}

1 个答案:

答案 0 :(得分:0)

您是否在清单中定义了精细和粗略的位置?

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
如果没有给出权限,

也会调用requestPermisiion方法。