2016-06-19 1 views
2

地図が読み込まれると、場所の変更とカメラの移動をもう一度チェックすることなく、ユーザーが尋ねてクリックしない限り、現在のユーザーの場所を表示することができますか?ボタンをクリックして現在の位置を取得するチュートリアルが多く見られましたが、どれも役に立たなかったのです。もう1つは、Deprecatedクラス(LocationClientなど)で古いものでした。地図が読み込まれると、現在の場所にカメラを移動する方法は、GoogleマップV2でですか?

助けていただけたら幸いです! ありがとう!

答えて

3

最初に位置が有効かどうかを確認します。あなたの位置が有効な場合、地図は自動的に読み込まれます。そうでなければここ

は、両方の事のために私のコード..です

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

      mMapFragment = (SupportMapFragment) getSupportFragmentManager() 
        .findFragmentById(R.id.map); 

      try { 
       manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
       if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
        Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
        startActivity(myIntent); 
        overridePendingTransition(R.anim.push_up_in, 
          R.anim.push_up_out); 
       } else { 
        mMapFragment.getMapAsync(this); 
        overridePendingTransition(R.anim.push_up_out, 
          R.anim.push_up_in); 

       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
    } 

@Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 
     if (isLocationEnabled(MapsActivity.this)) { 
      manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 
      mCriteria = new Criteria(); 
      bestProvider = String.valueOf(manager.getBestProvider(mCriteria, true)); 

      mLocation = manager.getLastKnownLocation(bestProvider); 
      if (mLocation != null) { 
       Log.e("TAG", "GPS is on"); 
       final double currentLatitude = mLocation.getLatitude(); 
       final double currentLongitude = mLocation.getLongitude(); 
       LatLng loc1 = new LatLng(currentLatitude, currentLongitude); 
       mMap.addMarker(new MarkerOptions().position(loc1).title("Your Current Location")); 
       mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(currentLatitude, currentLongitude), 15)); 
       mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); 
      } 
     } else { 
      if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) 
        != PackageManager.PERMISSION_GRANTED 
        && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) 
        != PackageManager.PERMISSION_GRANTED) { 
       return; 
      } 
      manager.requestLocationUpdates(bestProvider, 1000, 0, (LocationListener) this); 
     } 
     setupMap(); 
    } 

    private void setupMap() { 

     if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) 
       != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 
       android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      return; 
     } 

     AppSettings settings = AppSettings.getSettings(getApplication()); 
     String m = settings.getMapType(); 
     try { 
      if (m.contentEquals(null)) { 
       mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
      } else if (m.contentEquals("NORMAL")) { 
       mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
      } else if (m.contentEquals("HYBRID")) { 
       mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
      } else if (m.contentEquals("SATELLITE")) { 
       mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); 
      } else if (m.contentEquals("TERRAIN")) { 
       mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN); 
      } else { 
       mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
      } 
     } catch (NullPointerException e) { 
      e.printStackTrace(); 
     } 
     mMap.setMyLocationEnabled(true); 
     mMap.getUiSettings().setZoomControlsEnabled(true); 
     mMap.getUiSettings().setCompassEnabled(true); 
     mMap.getUiSettings().setIndoorLevelPickerEnabled(true); 
     mMap.setBuildingsEnabled(true); 
     mMap.setIndoorEnabled(true); 
    } 

このコードは、Googleマップv2のです。

GPS設定を有効にする場合、マップはonPause()状態になっていますので、アンドロイドのライフサイクルを処理してみてください。そのが必要な場合はOnResume()状態で、この行を入れ

..

@Override 
    protected void onResume() { 
     super.onResume(); 
     mMapFragment.getMapAsync(this); 
    } 
+0

は偉大な作業、あなたの先生ありがとうございました! – 2Stoned

関連する問題