谷歌地图不可移动

时间:2017-04-03 05:21:28

标签: java android google-maps

嘿伙计我正在创建一个应用程序,它获取用户的当前坐标,然后在地图上显示它们在活动上实现。

但我的问题是地图不可移动。我试过getUiSettings()。setScrollGesturesEnabled(true);但它不起作用。

守则如下:

public class Page1 extends AppCompatActivity implements OnMapReadyCallback {

private GoogleMap mMap;
String em,n;
private DrawerLayout mDrawer;
private Toolbar toolbar;
private NavigationView nvDrawer;
TextView tv1;
private static final int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 2;
protected LocationManager locationManager;
Location location;


private ActionBarDrawerToggle drawerToggle;


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

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

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    nvDrawer = (NavigationView) findViewById(R.id.nvView);
    setupDrawerContent(nvDrawer);
    // Find our drawer view
    mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerToggle = setupDrawerToggle();
    // Tie DrawerLayout events to the ActionBarToggle
    mDrawer.addDrawerListener(drawerToggle);
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
}

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_menu_option, menu);
    tv1= (TextView) nvDrawer.findViewById(R.id.textView5);
    Intent i=getIntent();
    em=i.getStringExtra("k");
    tv1.setText(em);
    return true;
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    Location nwLocation =getLocation(LocationManager.NETWORK_PROVIDER);
    if (nwLocation != null) {
        double latitude = nwLocation.getLatitude();
        double longitude = nwLocation.getLongitude();
        LatLng location = new LatLng(latitude,longitude);
        mMap.addMarker(new MarkerOptions().position(location).title("I am here"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
        float zoomLevel = 16; //This goes up to 21
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, zoomLevel));
        googleMap.getUiSettings().setScrollGesturesEnabled(true);
    }
    else {
        showSettingsAlert("Network");
    }
}

public Location getLocation(String provider) {
    if (locationManager.isProviderEnabled(provider)) {
        if(ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
        }
        else {
            if (locationManager != null) {
                location = locationManager.getLastKnownLocation(provider);
                return location;
            }
        }
    }
    return null;
}

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

                    Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();

                }
                else if(grantResults[0]==PackageManager.PERMISSION_DENIED) {
                    Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
                }
                else {

                    Toast.makeText(this, "Never Ask Again", Toast.LENGTH_SHORT).show();
                }
            }
            return;
        }
    }
}

public void showSettingsAlert(String provider) {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(
            Page1.this);

    alertDialog.setTitle(provider + " Settings");

    alertDialog.setMessage(provider + " is not enabled! Want to go to settings menu?");

    alertDialog.setPositiveButton("Settings",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    Page1.this.startActivity(intent);
                }
            });

    alertDialog.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

    alertDialog.show();
}

private ActionBarDrawerToggle setupDrawerToggle() {

    return new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open,  R.string.drawer_close);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (drawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    switch (item.getItemId()) {
        case R.id.m1:
            onMapReady(mMap);
            return true;
        case R.id.m2:
            Intent i=new Intent(this,AboutUs.class);
            startActivity(i);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

private void setupDrawerContent(final NavigationView navigationView) {
    navigationView.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    selectDrawerItem(menuItem);
                    View headerLayout = navigationView.getHeaderView(0);
                    return true;
                }
            });
}

public void selectDrawerItem(MenuItem menuItem) {
    // Create a new fragment and specify the fragment to show based on nav item clicked
    switch(menuItem.getItemId()) {
        case R.id.nav_first_fragment:
            mFirst(menuItem);
            mDrawer.closeDrawers();
            break;
        case R.id.nav_second_fragment:
            /*Intent i = new Intent(Page1.this, ShowInfo.class);
            i.putExtra("j", em);
            startActivity(i);*/
            Toast.makeText(this, "Location Updated", Toast.LENGTH_SHORT).show();
            int a=0;
            Intent i=getIntent();
            a=i.getIntExtra("u",0);
            Location nwLocation =getLocation(LocationManager.NETWORK_PROVIDER);
            if (nwLocation != null) {
                double latitude = nwLocation.getLatitude();
                double longitude = nwLocation.getLongitude();
                updateLocation(a,latitude,longitude);
            }
            else {
                showSettingsAlert("Network");
            }
            break;
        case R.id.sub1:
            startActivity(new Intent(this, ShowInfo.class));
            break;
        case R.id.sub2:
            startActivity(new Intent(this, LoginPage.class));
            finish();
            break;
        default:
            mFirst(menuItem);
            mDrawer.closeDrawers();
    }

    menuItem.setChecked(true);
    setTitle(menuItem.getTitle());
    mDrawer.closeDrawers();
}

public void updateLocation(int userid,double xCoordinate,double yCoordinate){
    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("userId", userid);
        jsonObject.put("deviceName", "Mi4i");
        jsonObject.put("xCoordinate",xCoordinate);
        jsonObject.put("yCoordinate",yCoordinate);
        jsonObject.toString();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    final OkHttpClient client = new OkHttpClient();
    RequestBody body = RequestBody.create(JSON, String.valueOf(jsonObject));
    final Request request = new Request.Builder()
            .url("http://172.31.4.91:8090/rest/saveLocation")
            .post(body)
            .build();
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            e.printStackTrace();
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            } /*else {

            }*/
        }
    });
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}

}

此活动的用户界面是: -

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.pb.larcenytest.Page1" >

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <!-- The main content view where fragments are loaded -->
    <FrameLayout
        android:id="@+id/flContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

<include
    layout="@layout/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

<android.support.design.widget.NavigationView
android:id="@+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
android:background="@android:color/white"
app:menu="@menu/drawer_view" />
</android.support.v4.widget.DrawerLayout>

</fragment>

1 个答案:

答案 0 :(得分:0)

您从网络提供商处获取位置,这需要时间

如何添加location.LocationListener

A = [3 4 0; 2 3 7; 45 7 0]
n = 1; 
X = zeros(3,1); 
comb(n, X, A); 

%function to calculate all combinations. 
function X = comb(n, X, A)
   if (n > 3)
      X
      return 
   end 
   for i = 1:3 
      X(n) = A(n, i); 
      comb(n + 1, X, A); 
   end
end