2017-02-21 7 views
1
public class MapViewFragment extends Fragment implements LocationListener 

    public MapViewFragment() { 
     // Required empty public constructor 
    } 


    public static MapViewFragment newInstance(String tabSelected) { 
     MapViewFragment fragment = new MapViewFragment(); 
     Bundle args = new Bundle(); 
     args.putString(Constants.FRAG_D, tabSelected); 
     fragment.setArguments(args); 

     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if (getArguments() != null) { 
     } 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     MapsInitializer.initialize(getActivity()); 

     if (mapView != null) { 
      mapView.onCreate(savedInstanceState); 
     } 
     initializeMap(); 
    } 

    private void initializeMap() { 
     if (googleMap == null && mapsSupported) { 
      mapView = (MapView) getActivity().findViewById(R.id.map); 
      googleMap = mapView.getMap(); 
      //setup markers etc... 
     } 
    } 





    @Override 
    public void onResume() { 
     super.onResume(); 
     Marker m1,m2,m3; 
     m1 = googleMap.addMarker(new MarkerOptions() 
       .position(new LatLng(38.609556, -1.139637)) 
       .anchor(0.5f, 0.5f) 
       .title("Title1") 
       .snippet("Snippet1")); 


     m2 = googleMap.addMarker(new MarkerOptions() 
       .position(new LatLng(40.4272414,-3.7020037)) 
       .anchor(0.5f, 0.5f) 
       .title("Title2") 
       .snippet("Snippet2")); 

     mapView.onResume(); 
     initializeMap(); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     mapView.onPause(); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     mapView.onDestroy(); 
    } 

    @Override 
    public void onLowMemory() { 
     super.onLowMemory(); 
     mapView.onLowMemory(); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 

    } 

マイコードは上記のとおりです。私はAndroidスタジオで手動で2つのマーカーを設定することができますが、現在の場所から位置を取得する1つのマーカーです。 ヘルプやアイデアはありますか?フラグメントを使用するAndroidで現在の場所を取得したい

答えて

1

まず最初は、最初にこれを試してみてください。

googleMap.setMyLocationEnabled(true); 

これは、それが有効になっている場合は、あなたの場所を見るようになりますページの右上部にある矢印が表示されます。 このボタンは、onMyLocationButtonClick() google apiの機能をトリガーします。

そして、現在位置情報取得:

Criteria criteria = new Criteria(); 
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    String provider = locationManager.getBestProvider(criteria, true); 
    Location location = locationManager.getLastKnownLocation(provider); 
    double latitude = location.getLatitude(); 
    double longitude = location.getLongitude(); 
    myPosition = new LatLng(latitude, longitude); 

が最後にそれであなたのマーカーを追加します。https://developers.google.com/maps/documentation/android-api/location

+1

おかげで答えをkoksalb:例外および詳細については

googleMap.addMarker(new MarkerOptions().position(myPosition).title("Marker")); 

を –

関連する問題