2016-08-03 60 views
-2
public class map2 extends AppCompatActivity implements OnMapReadyCallback, GoogleMap.OnMyLocationChangeListener { 

    private static final String TAG =null ; 
    private GoogleMap mGoogleMap; 
    private Geocoder mGeocoder; 


    String Startlongitude,Endlongitude; 
    String Startlattitude,Endlattitude; 
    double lon,Eat,Elot; 
    double lat; 
    LatLng latlon,latlon2; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_map); 
     SupportMapFragment mSupportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
     mSupportMapFragment.getMapAsync(this); 

     mGeocoder = new Geocoder(this, Locale.getDefault()); 
     Bundle b = getIntent().getExtras(); 
      String Array=b.getString("ITEM_EXTRA"); 
     String Tripname = getIntent().getExtras().getString("Trip"); 
     List<LatLng> routeArray = new ArrayList<>(); 
     try { 
      JSONArray jO=new JSONArray(Array); 
      for(int i=0;i<jO.length();i++) 
      { 
       JSONObject tripObject=jO.getJSONObject(i); 
       String Trips=tripObject.getString("TripNO"); 
       JSONObject msg=tripObject.getJSONObject("Trips"); 
       if(Trips.equals(Tripname)) 
       { 
        JSONArray msgObject=msg.getJSONArray("Trip1"); 
        for(int j=0;j<msgObject.length();j++) 
        { 
         JSONObject d=msgObject.getJSONObject(j); 
         Startlongitude=d.getString("SfltLogitude"); 
         Startlattitude=d.getString("Sfltlattitude"); 
         Endlongitude = d.getString("EfltLogitude"); 
         Endlattitude = d.getString("Efltlattitude"); 
         Eat=Double.parseDouble(Endlattitude.trim()); 
         Elot=Double.parseDouble(Endlongitude.trim()); 

         lat=Double.parseDouble(Startlattitude.trim()); 
         lon=Double.parseDouble(Startlongitude.trim()); 
         latlon= new LatLng(lat, lon); 
         latlon2=new LatLng(Eat,Elot); 

         PolylineOptions polyLineOptions = new PolylineOptions(); 
         polyLineOptions.addAll(latlon,latlon2); 
         polyLineOptions.width(2); 
         polyLineOptions.color(Color.BLUE); 
         mGoogleMap.addPolyline(polyLineOptions); 

         break; 
        } 
       } 
       else{ 
        String task=tripObject.getString("Trips"); 
       } 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 






    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.normal: 
       mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
       break; 
      case R.id.satellite: 
       mGoogleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); 
       break; 
      case R.id.terrain: 
       mGoogleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN); 
       break; 
      case R.id.hybrid: 
       mGoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
       break; 
      case R.id.none: 
       mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NONE); 
       break; 
     } 

     return true; 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mGoogleMap = googleMap; 
     mGoogleMap.getUiSettings().setZoomControlsEnabled(true); 
     mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true); 
     mGoogleMap.setMyLocationEnabled(true); 
     mGoogleMap.setOnMyLocationChangeListener(this); 


    } 

    @Override 
    public void onMyLocationChange(Location location) { 

    } 
} 

這裏我從JsonServer獲取經度和經度,並將其轉換爲雙打......但iam無法繪製谷歌地圖中的路徑?? IT向我顯示這個錯誤「Polyline com。空對象引用「上的google.android.gms.maps.GoogleMap.addPolyline(com.google.android.gms.maps.model.PolylineOptions)」如何在Android中的谷歌地圖使用折線?

+0

mGoogleMap爲空,這就是爲什麼空指針異常。發佈整個代碼 –

+0

請立即檢查。 – joe

回答

0

當地圖尚未準備就緒時,您正在設置地圖上的折線。因此,請移動您在onMapReady()方法中設置折線的代碼。

像:

public class map2 extends AppCompatActivity implements OnMapReadyCallback, GoogleMap.OnMyLocationChangeListener { 

private static final String TAG =null ; 
private GoogleMap mGoogleMap; 
private Geocoder mGeocoder; 

String Startlongitude,Endlongitude; 
String Startlattitude,Endlattitude; 
double lon,Eat,Elot; 
double lat; 
LatLng latlon,latlon2; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_map); 
    SupportMapFragment mSupportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
    mSupportMapFragment.getMapAsync(this); 


} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.normal: 
      mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
      break; 
     case R.id.satellite: 
      mGoogleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); 
      break; 
     case R.id.terrain: 
      mGoogleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN); 
      break; 
     case R.id.hybrid: 
      mGoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
      break; 
     case R.id.none: 
      mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NONE); 
      break; 
    } 

    return true; 
} 

@Override 
public void onMapReady(GoogleMap googleMap) { 
    mGoogleMap = googleMap; 
    mGoogleMap.getUiSettings().setZoomControlsEnabled(true); 
    mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true); 
    mGoogleMap.setMyLocationEnabled(true); 
    mGoogleMap.setOnMyLocationChangeListener(this); 

    mGeocoder = new Geocoder(this, Locale.getDefault()); 
    Bundle b = getIntent().getExtras(); 
    String Array=b.getString("ITEM_EXTRA"); 
    String Tripname = getIntent().getExtras().getString("Trip"); 
    List<LatLng> routeArray = new ArrayList<>(); 
    try { 
     JSONArray jO=new JSONArray(Array); 
     for (int i = 0; i < jO.length(); i++) 
     { 
      JSONObject tripObject=jO.getJSONObject(i); 
      String Trips = tripObject.getString("TripNO"); 
      JSONObject msg = tripObject.getJSONObject("Trips"); 
      if (Trips.equals(Tripname)) 
      { 
       JSONArray msgObject=msg.getJSONArray("Trip1"); 
       for (int j = 0; j < msgObject.length(); j++) 
       { 
        JSONObject d = msgObject.getJSONObject(j); 
        Startlongitude = d.getString("SfltLogitude"); 
        Startlattitude = d.getString("Sfltlattitude"); 
        Endlongitude = d.getString("EfltLogitude"); 
        Endlattitude = d.getString("Efltlattitude"); 
        Eat=Double.parseDouble(Endlattitude.trim()); 
        Elot=Double.parseDouble(Endlongitude.trim()); 

        lat = Double.parseDouble(Startlattitude.trim()); 
        lon = Double.parseDouble(Startlongitude.trim()); 
        latlon = new LatLng(lat, lon); 
        latlon2 = new LatLng(Eat,Elot); 

        PolylineOptions polyLineOptions = new PolylineOptions(); 
        polyLineOptions.addAll(latlon,latlon2); 
        polyLineOptions.width(2); 
        polyLineOptions.color(Color.BLUE); 
        mGoogleMap.addPolyline(polyLineOptions); 

        break; 
       } 
      } 
      else { 
       String task = tripObject.getString("Trips"); 
      } 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 

@Override 
public void onMyLocationChange(Location location) { 

} 
} 
+0

Thankyou ....它沒有給出以前的錯誤,但....仍然沒有繪製路徑...在調試器中,我檢查了它不會繼續「mGoogleMap.addPolyline(polyLinesOptions);」? – joe