2016-09-21 16 views
0

私は自分のNavドロワの断片の中にGoogleマップを持っていて、それをアクティビティに切り替えたくありません。Google Mapsに線が描画されていません

lineOptions = new PolylineOptions() 
       .add(new LatLng(33.927085040000001, -118.39129683)) 
       .add(new LatLng(33.927085969382027, -118.39129820011991)) 
       .add(new LatLng(33.927081024586677, -118.39130352185116)) 
       .add(new LatLng(33.927077084498386, -118.39130986277655)) 
       .add(new LatLng(33.92707405365519, -118.39131496827862)) 
       .add(new LatLng(33.927066722586623, -118.39131750469446)) 
       .add(new LatLng(33.927068689880947, -118.39131823397425)) 
       .add(new LatLng(33.927068030419839, -118.39131796208994)) 
       .add(new LatLng(33.927065982966624, -118.39132090766913)) 
       .add(new LatLng(33.927065258415212, -118.3913193654964)) 
       .width(5) 
       .color(Color.RED); 
     line = mMap.addPolyline(lineOptions); 

しかし、これら二つの点の間に線を追加されていない。しかし、単に地図上に線を描画するために、私は、次のコードビットを追加しました。私はこのSOの質問Drawing a line/path on Google Mapsからコードのビットを得て、人はそれが働いたと言いました。だからどこが間違っているのか分からない。私がハードコードされた値を持っている理由は、これを理解した後、JSONレスポンスから複数のルートをプロットするためです。 公式ガイドから直接...

public class ThirdFragment extends Fragment implements OnMapReadyCallback { 
    View myView; 
    private GoogleMap mMap; 
    MapFragment mapFrag; 
    private Polyline line; 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     myView = inflater.inflate(R.layout.third_layout, container, false); 
     return myView; 
    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 

     mapFrag = (MapFragment) getChildFragmentManager().findFragmentById(R.id.map); 
     mapFrag.getMapAsync(this); 
     //mapFrag.onResume(); 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 


     if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
      line = mMap.addPolyline(new PolylineOptions() 
        .add(new LatLng(33.8031, 118.0726), new LatLng(33.9192, 118.4165)) 
        .width(5) 
        .color(Color.RED)); 
      mMap.setMyLocationEnabled(true); 
      Criteria criteria = new Criteria(); 
      LocationManager locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE); 
      String provider = locationManager.getBestProvider(criteria, false); 
      Location location = locationManager.getLastKnownLocation(provider); 
      double lat = location.getLatitude(); 
      double lng = location.getLongitude(); 
      LatLng coordinate = new LatLng(lat, lng); 
      CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 13); 
      mMap.animateCamera(yourLocation); 


     } else { 
      final AlertDialog alertDialogGPS = new AlertDialog.Builder(getActivity()).create(); 

      alertDialogGPS.setTitle("Info"); 
      alertDialogGPS.setMessage("Looks like you have not given GPS permissions. Please give GPS permissions and return back to the app."); 
      alertDialogGPS.setIcon(android.R.drawable.ic_dialog_alert); 
      alertDialogGPS.setButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 

        Intent intentSettings = new Intent(Settings.ACTION_APPLICATION_SETTINGS); 
        startActivity(intentSettings); 
        alertDialogGPS.dismiss(); 
       } 

      }); 

      alertDialogGPS.show(); 
     } 

    } 
    @Override 
    public void onResume() { 
     super.onResume(); 
     if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 

      mapFrag.getMapAsync(this); 
     } 
     else { 
     } 
    } 
} 
+1

コードは大丈夫でしょうか、カメラのズームのためにラインが見えないのでしょうか?あなたが世界の景色にズームアウトすると、ラインを見ることができないことは確かですか?さらに、コードが呼び出されていることを確認してください(デバッグモード)? –

+0

ズームアウトしてもまだ表示されません。さらに、私の現在の位置から線が始まります。 'line = mMap.addPolyline'の後のすべてが呼び出されるので、コードを呼び出す必要があります。 –

+0

私は愚かな気持ちで、私はグーグル・グーグルと協力して、118年代の前にサインをつけるとは思わなかった。 –

答えて

1

私はリストに追加考える呼び出すための最良の方法ではありません:ここでは、すべてのコードがある

PolylineOptions rectOptions = new PolylineOptions() 
     .add(new LatLng(37.35, -122.0)) 
     .add(new LatLng(37.45, -122.0)) // North of the previous point, but at the same longitude 
     .add(new LatLng(37.45, -122.2)) // Same latitude, and 30km to the west 
     .add(new LatLng(37.35, -122.2)) // Same longitude, and 16km to the south 
     .add(new LatLng(37.35, -122.0)); // Closes the polyline. 

// Get back the mutable Polyline 
Polyline polyline = myMap.addPolyline(rectOptions); 

あなたが見ることができるように、各ポイントを追加しますadd関数を使用します。 longのサイズに関しては、必要なものを置くことができ、gmapsは必要なところで小数を切ります(約5番目の数字)。これを試してから、座標を変更してみてください。 LatはY軸です(アフリカは〜負の緯度で、値は-90〜90です)。LonはX軸です(USAは負のlonで-180〜180の値)。

+0

興味深いことに私が使用している緯度と経度は本当に正確ですから、5番目の数字に切り下げられてから、ラインを見るには4〜5より多くかかるでしょう。 –

+0

よく待ってください、5番目の小数点以下を切り捨てて、行を描画する必要がある値と異なる場合は、4-5小数点が1点になります。それは今働いていますか?そうでない場合は、コードを更新してください、私は今失われています:D –

+1

私はそれを更新したので、私はズームしているときには小さなドットです....私は線を太くする必要がありますね。 –

関連する問題