2017-10-02 7 views
0

Google지도에서 포인트 그룹을 연결하는 폴리 라인을 추가하려고합니다. 앱을 빌드 할 때 오류가 발생하지 않지만 폴리선이 나타나지 않습니다 .isvisible() 메소드를 호출하면 반환됩니다. 당신은 아마 당신의 응용 프로그램에 ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATION을 부여하지 않았기 때문에 true.Please 도움말폴리 라인이 Google지도에 표시되지 않습니다.

@Override 
public void onMapReady(GoogleMap googleMap) { 
    // new LongOperation().execute(""); 

    /* googleMap.setMapStyle(
      MapStyleOptions.loadRawResourceStyle(
        this, R.raw.mapjson));*/ 

    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. 
     return; 
    } 
    googleMap.setMyLocationEnabled(true); 
    googleMapnew = googleMap; 
    Polyline polyline1 = googleMap.addPolyline(new PolylineOptions() 
      .clickable(true) 
      .add(
        new LatLng(13.034063, 77.567006), 
        new LatLng(13.034087, 77.568629), 
        new LatLng(13.034202, 77.569431), 
        new LatLng(13.034371, 77.570103), 
        new LatLng(13.034535, 77.570121), 
        new LatLng(13.036526, 77.570489), 
        new LatLng(13.037719, 77.570545), 
        new LatLng(13.038252, 77.570039), 
        new LatLng(13.039849, 77.570028), 
        new LatLng(13.040153, 77.569229), 
        new LatLng(13.040640, 77.568512), 
        new LatLng(13.041071, 77.567996), 
        new LatLng(13.041780, 77.567894)) 
    .color(Color.GREEN) 
    .width(66)); 
    Log.e("polylog", String.valueOf(polyline1.isVisible())); 
    Log.e("polylog", String.valueOf(polyline1)); 

} 

답변

2

문제는,

googleMap.setMyLocationEnabled(true); 

라인입니다. 당신은 당신의 안드로이드 장치의 Applications 메뉴를 통해 응용 프로그램에 수동으로 부여하거나 검사를 구현하고 Official Documentation처럼 권한을 부여하고, 예를 들어, Daniel Nugentthis 대답, 그런 일해야

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback { 

    private static final int LOCATION_PERMISSION_REQUEST_CODE = 101; 
    private GoogleMap mGoogleMap; 
    private MapFragment mMapFragment; 

    private void makeLocationPermissionRequest() { 
     ActivityCompat.requestPermissions(this, 
       new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE); 
    } 

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

     mMapFragment = (MapFragment) getFragmentManager() 
       .findFragmentById(R.id.map_fragment); 
     mMapFragment.getMapAsync(this); 
    } 

    private void showPolyline() { 

     mGoogleMap.setMyLocationEnabled(true); 

     Polyline polyline1 = mGoogleMap.addPolyline(new PolylineOptions() 
       .clickable(true) 
       .add(
         new LatLng(13.034063, 77.567006), 
         new LatLng(13.034087, 77.568629), 
         new LatLng(13.034202, 77.569431), 
         new LatLng(13.034371, 77.570103), 
         new LatLng(13.034535, 77.570121), 
         new LatLng(13.036526, 77.570489), 
         new LatLng(13.037719, 77.570545), 
         new LatLng(13.038252, 77.570039), 
         new LatLng(13.039849, 77.570028), 
         new LatLng(13.040153, 77.569229), 
         new LatLng(13.040640, 77.568512), 
         new LatLng(13.041071, 77.567996), 
         new LatLng(13.041780, 77.567894)) 
       .color(Color.GREEN) 
       .width(66)); 

     mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(13.034063, 77.567006), 12)); 
    } 

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

     if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      int locationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION); 
      if (locationPermission != PackageManager.PERMISSION_GRANTED) { 
       makeLocationPermissionRequest(); 
      } else { 
       showPolyline(); 
      } 
     } else { 
      showPolyline(); 
     } 
} 

    @Override 
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 
     switch (requestCode) { 
      case LOCATION_PERMISSION_REQUEST_CODE: { 
       // If request is cancelled, the result arrays are empty. 
       if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        showPolyline(); 
       } else { 
       } 
       return; 
      } 
     } 
    } 

} 

을 그리고 잊지 마세요 당신의 AndroidManifest.xml 파일에

를 추가 할 수 있습니다. 그리고 당신은 그런 것을 얻었습니다 :

enter image description here