2017-04-03 155 views
-1

嘿家伙我正在创建一个应用程序,获取用户的当前坐标,然后显示他们在活动上实施的地图上。谷歌地图是不可移动的

但我的问题是,地图是不可移动的。我试过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); 
} 

}

这项活动的UI是: -

<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> 
+0

你可以发布UI部分吗?你在genymotion或真正的设备上测试过吗? – Steve

+0

我在多个真实设备上测试过它,但它不工作。 – pragam

+0

你可以请张贴UI部分? – Steve

回答

0

您是从网络服务提供者得到的位置,这需要时间

怎么样添加location.LocationListener

public class Page1 extends AppCompatActivity implements OnMapReadyCallback,location.LocationListener { 



    @Override 
      public void onLocationChanged(Location location) 
      { 
    CameraPosition position = new CameraPosition.Builder() 
           .target(new LatLng(Location.getLatitude, Location.getLongitude)) 
           .zoom(17).build(); 
         mMap.animateCamera(CameraUpdateFactory.newCameraPosition(position)); 
      } 
    } 
+0

此功能只会自动更新我的位置。它不会解决不动的地图问题。 – pragam