1
私の簡単なプログラムは自分の位置を表示し、スピナーで選択した場所にポリラインを描きます。現在のところ私の場所だけが表示されていますが、理由はわかりません。おそらく、問題はAndroid Marshmallowに導入されたアクセス許可です。ここで が私のコードです:だから私の単純なプログラムは自分の位置を表示しますが、スピナーで選択した場所にポリラインを表示しません
public class MapsActivity extends FragmentActivity {
private final LatLng MainHousing = new LatLng(49.83511, 24.01446);
private final LatLng FirstHousing = new LatLng(49.83533, 24.01004);
private final LatLng FifthHousing = new LatLng(49.835, 24.00824);
private static final int REQUEST_LOCATION = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
};
GoogleMap googleMap;
Location location;
LatLng myPosition;
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
List<String> list = new ArrayList<>();
list.add("Головний корпус");
list.add("I корпус");
list.add("V корпус");
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
showMyLocation();
}
private void showMyLocation() {
// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment fm = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
googleMap = fm.getMap();
// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
PERMISSIONS_STORAGE, REQUEST_LOCATION);
}
location = locationManager.getLastKnownLocation(provider);
if (location != null) {
// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
myPosition = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions().position(myPosition).title("Start"));
final PolylineOptions pOptions = new PolylineOptions();
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (spinner.getSelectedItemPosition()) {
case 0:
pOptions.add(myPosition).add(MainHousing).width(5).color(Color.BLUE).geodesic(true);
googleMap.addPolyline(pOptions);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(MainHousing, 13));
break;
case 1:
pOptions.add(myPosition).add(FirstHousing).width(5).color(Color.BLUE).geodesic(true);
googleMap.addPolyline(pOptions);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(FirstHousing, 13));
break;
case 2:
pOptions.add(myPosition).add(FifthHousing).width(5).color(Color.BLUE).geodesic(true);
googleMap.addPolyline(pOptions);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(FifthHousing, 13));
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
switch (requestCode) {
case REQUEST_LOCATION:
if (grantResults.length > 0 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
} else {
// Permission Denied
Toast.makeText(MapsActivity.this, "LOCATION Denied", Toast.LENGTH_SHORT)
.show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
、私の問題は何ですか?私はアンドロイドが初めてだから愚かなことになるかもしれない。前もって感謝します。