2017-04-19 64 views
1

我是新来的使用谷歌地图API和已经在片段中实现了地图。我在地图上添加了一个标记。现在我想让我的位置像谷歌地图一样弹出蓝点。任何人都可以帮我完成这个吗?非常感谢。我需要填写什么来获取我的位置?像蓝点谷歌有

我的Java文件:

import android.support.v4.app.FragmentActivity; 
import android.os.Bundle; 

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener; 
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; 

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

    // Add a marker in Sydney and move the camera 
    LatLng some = new LatLng(80.153, 15.2620); 
    mMap.addMarker(new MarkerOptions().position(some).title("Marker in some")); 
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(some, 18)); 

} 
} 

清单文件有这个ACCESS_FINE_LOCATION

回答

2

您需要启用我的位置层

  1. 检查您的应用程序获取权限ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATION

  2. 启用我的位置层,使用 mMap.setMyLocationEnabled(true);

如果你不这样做一步1,你会得到SecurityException。你应该重写ActivityCompat.OnRequestPermissionsResultCallback所以最终代码应该是这样的:

// Check for permission to access Location 
private boolean checkLocationPermission() { 
    Log.d(TAG, "checkPermission()"); 
    // Ask for permission if it wasn't granted yet 
    return (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 
      == PackageManager.PERMISSION_GRANTED); 
} 

// Asks for permissions 
private void askPermission() { 
    Log.d(TAG, "askPermission()"); 
    ActivityCompat.requestPermissions(
      this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
      PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION 
    ); 
} 

// Reaction to whether user granted or denied permissions 
@Override 
public void onRequestPermissionsResult(
     int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
    Log.d(TAG, "onRequestPermissionsResult()"); 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    switch (requestCode) { 
     case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: { 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       // Permission granted 
       if (checkLocationPermission()) 
        mMap.setMyLocationEnabled(true); 
      } else { 
       // Permission denied 
       // TODO 
      } 
      break; 
     } 
    } 
} 

onMapReady做到这一点:

if (checkLocationPermission()) 
     mMap.setMyLocationEnabled(true); 
    else 
     askPermission(); 

下面是引用:

https://developers.google.com/maps/documentation/android-api/location#my-location

https://developers.google.com/maps/documentation/android-api/location#runtime-permission

相关问题