2017-06-02 10 views
0

この問題は解決できません。マップを使ってフラグメントを作成しようとしていますが、これを行っています。静的なLong、Lat値を追加するとマップにこのマップが表示されますが、実行時にローカライゼーションを自動化しようとすると、アプリケーションはマップを開き、特定のLongLand値を表示しません。現在のローカリゼーションを判断し、アンドロイドのGoogleMapsApiにマーカーを追加する方法

これを行う方法?私はLocalizationManagerを使用しています長いと後期取得するための

は、もちろん、私はこの問題は同期データから来ると思うマニフェストに権限を追加しても、ランタイム

public class LocalizationFragment extends Fragment implements OnMapReadyCallback { 


private GoogleMap map; 
MapView mapView; 
View mview; 
private double latitude, longtitude; 
private Handler handler; 
private Runnable runnable; 


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

@Override 
public void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    mview = inflater.inflate(R.layout.fragment_localization, container, false); 
    handler = new Handler(); 
    getLocation(); 
    if (ContextCompat.checkSelfPermission(getContext(), android.Manifest.permission.CAMERA) 
      != PackageManager.PERMISSION_GRANTED) { 
     ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 13); 
    } 
    if (ContextCompat.checkSelfPermission(getContext(), android.Manifest.permission.CAMERA) 
      != PackageManager.PERMISSION_GRANTED) { 
     ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 11); 
    } 
    return mview; 
} 

@OnClick(R.id.button) 
public void buttonStart() { 
    getLocation(); 
} 

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    mapView = (MapView) mview.findViewById(R.id.mapView); 
    if (mapView != null) { 
     mapView.onCreate(null); 
     mapView.onResume(); 
     mapView.getMapAsync(this); 
    } 
} 

private void getLocation() { 
    LocationManager locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE); 

    LocationListener locationListener = new LocationListener() { 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onProviderEnabled(String provider) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onLocationChanged(Location location) { 
      // TODO Auto-generated method stub 
      latitude = location.getLatitude(); 
      longtitude = location.getLongitude(); 
     } 
    }; 
    if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission 
      (getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     return; 
    } 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 

} 

@Override 
public void onMapReady(final GoogleMap googleMap) { 
    MapsInitializer.initialize(getContext()); 
    map = googleMap; 
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
    runnable = new Runnable() { 
     @Override 
     public void run() { 
      getLocation(); 
      googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longtitude)).title("Statue").snippet("Something")); 
      CameraPosition liberty = CameraPosition.builder().target(new LatLng(latitude, longtitude)).zoom(15).bearing(0).tilt(45).build(); 
      googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(liberty)); 
      handler.postDelayed(this, 5000); 
     } 
    }; 
    } 
} 

答えて

1

マーカーを追加する必要があります。あなたのrunnable。

runOnUiThread(new Runnable() { 

        @Override 
        public void run() { 
        googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longtitude)).title("Statue").snippet("Something")); 
        } 
       }); 
1

をご確認ください。マーカーを追加した後に場所がある場合は意味がなく、場所がなく、地図上にマーカーがありません。アクションをマーカーに追加してくださいonLocationChangedに

+0

IAMがしようとしたが、その後、彼は任意のマーカー – Rodriquez

+0

せずにこの方法をマップを生成:requestLocationUpdatesいくつかの時間は場所を得ることができない、イムは、3年前にそれを試してみました。この時点でデバッグして、場所を確認してください。もう1つ問題がある場合は、requestRocationUpdatesをOnRequestPermissionResultに配置して、場所を取得する権限があることを確認する必要があります – GiaLe

関連する問題