折线在Google地图中不可见 [英] polyline not visible in google maps
问题描述
我试图在谷歌地图中添加连接一组点的多段线。当我构建应用程序时,我没有收到任何错误,但折线不出现。如果我调用isvisible()方法,它会返回true.Please帮助
p> @Override
public void onMapReady(GoogleMap googleMap){
// new LongOperation()。execute( );
$ b $ * 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:考虑调用
// ActivityCompat#requestPermissions
//在这里请求丢失的权限,然后重写
// public void onRequestPermissionsResult(int requestCode,String [] permissions,
// int [] grantResults)
//处理用户授予权限的情况。有关更多详细信息,请参阅文档
//获取ActivityCompat#requestPermissions。
return;
}
googleMap.setMyLocationEnabled(true);
googleMapnew = googleMap;
Polyline polyline1 = googleMap.addPolyline(新的PolylineOptions()
.clickable(true)
.add(
新的LatLng(13.034063,77.567006),
新的LatLng (13.034087,77.568629),
新LatLng(13.034202,77.569431),
新LatLng(13.034371,77.570103),
新LatLng(13.034535,77.570121),
新LatLng(13.036526,新增LatLng(13.037719,77.570545),
新LatLng(13.038252,77.570039),
新LatLng(13.039849,77.570028),
新LatLng(13.040153,77.569229) ,
新LatLng(13.040640,77.568512),
新LatLng(13.041071,77.567996),
新LatLng(13.041780,77.567894))
.color(Color.GREEN)
.width(66));
Log.e(polylog,String.valueOf(polyline1.isVisible()));
Log.e(polylog,String.valueOf(polyline1));
在
googleMap.setMyLocationEnabled(true);
一行,因为您可能未授予 ACCESS_FINE_LOCATION
和 ACCESS_COARSE_LOCATION
添加到您的应用中。您应该通过Android设备的应用程序
菜单手动将其授予您的应用程序,或实施检查和授予权限,如
I am trying to add polyline connecting a group of points in google maps.When i build the app I am not getting any error but polyline does not appear.If i call isvisible() method it returns true.Please Help
@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));
}
The issue is in
googleMap.setMyLocationEnabled(true);
line, because you probably didn't grant ACCESS_FINE_LOCATION
and ACCESS_COARSE_LOCATION
to your app. You should grant it manually to your application via Applications
menu of your Android device or implement checking and grant permissions like in Official Documentation and, for example, this answer of Daniel Nugent, something like that:
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;
}
}
}
}
And don't forget to add
to your AndroidManifest.xml
file. And you got something like that:
这篇关于折线在Google地图中不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!