2016-09-21 244 views
0

我的Google导航片段内有谷歌地图,不想将其切换为活动。然而,仅仅简单地在地图上绘制一条线,我添加以下代码位:未在Google地图上绘制线条

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

我觉得很愚蠢,我使用了Google坐标系,并没有想到在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); 

正如你看到的,添加的每个点与添加功能。关于长的大小,你可以放任何你想要的,gmaps会减少它想要的小数(大约5号)。试试这个,然后尝试改变坐标。 记住Lat是Y轴(非洲是负拉特,值从-90到90),Lon是X轴(USA是负lon,值从-180到180)。

+0

嗯有趣。是的,我正在使用的经纬度真的很精确,所以我猜想从第五个数字减少到第四个数字需要大于4-5。 –

+0

好好等一下,小数点后4-5位是小数点,如果你切到小数点后第五位,并且有不同的值必须绘制!它现在工作吗?如果不是,请更新您的代码,我现在丢失了:D –

+1

因此,我更新了它,哈哈,它是一个小点,当你在我的变焦....我想我应该使线更粗。 –