2015-02-23 80 views
0

我想繪製折線爲googlemap,它工作正常到Android版本4.4(kitkat),但它不工作在Android 5.0(棒棒糖)。還有什麼做棒棒糖的工作。請你幫我鋤解決這個問題,這是我的谷歌地圖代碼谷歌地圖折線不工作在Android版本5.0(棒棒糖)

public class MapLocation extends FragmentActivity implements OnMarkerClickListener { 

    GoogleMap map; 
    ArrayList<LatLng> markerPoints; 

    LatLng origin,dest1,dest2,dest3;  
    Marker marker1,marker2,marker3; 

    PolylineOptions lineOptions = new PolylineOptions(); 

    Polyline line ; 
    int pos; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.gmap_direction); 

     markerPoints = new ArrayList<LatLng>(); 

     // Getting reference to SupportMapFragment of the activity_main 
     SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); 

     // Getting Map for the SupportMapFragment 
     map = fm.getMap();  

     if(map!=null){ 

      // Enable MyLocation Button in the Map 
      map.setMyLocationEnabled(true);  

      // Getting LocationManager object from System Service LOCATION_SERVICE 
      LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 

      // Creating a criteria object to retrieve provider 
      Criteria criteria = new Criteria(); 

      // Getting the name of the best provider 
      String provider = locationManager.getBestProvider(criteria, true); 

      // Getting Current Location 
      Location location = locationManager.getLastKnownLocation(provider); 
      pos = getIntent().getIntExtra("pos",0); 
      try{ 
      switch (pos) { 

       case 1: 

        dest2 = new LatLng(18.545981, 73.902132); 
        marker2 = map.addMarker(new MarkerOptions().position(dest2).title("Vivero International Preschool").snippet("PUNE,KALYANINAGAR").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))); 
        break; 
       case 2: 

        dest2 = new LatLng(17.429122, 78.412745); 
        marker2 = map.addMarker(new MarkerOptions().position(dest2).title("Vivero International Preschool").snippet("Jubilee Hills").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))); 
        break; 
       case 3: 

        dest2 = new LatLng(19.115177, 72.909486); 
        marker2 = map.addMarker(new MarkerOptions().position(dest2).title("Vivero International Preschool").snippet("MUMBAI, POWAI").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))); 
        break; 
       case 4: 

        dest2 = new LatLng(18.545981, 73.902132); 
        marker2 = map.addMarker(new MarkerOptions().position(dest2).title("Vivero International Preschool").snippet("PUNE,MAGARPATTA").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))); 
        break; 
       case 5: 

        dest2 = new LatLng(12.924438, 77.631537); 
        marker2 = map.addMarker(new MarkerOptions().position(dest2).title("Vivero International Preschool").snippet("Koramangala").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))); 
        break; 
       case 6: 

        dest2 = new LatLng(12.9044019,77.6690773); 
        marker2 = map.addMarker(new MarkerOptions().position(dest2).title("Vivero International Preschool").snippet("Sarjapur").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))); 
        break; 
       case 7: 

        dest2 = new LatLng(12.972442, 77.639195); 
        marker2 = map.addMarker(new MarkerOptions().position(dest2).title("Vivero International Preschool").snippet("Indiranagar").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))); 
        break; 
       case 8: 

        dest2 = new LatLng(12.968719, 77.747883); 
        marker2 = map.addMarker(new MarkerOptions().position(dest2).title("Vivero International Preschool").snippet("Whitefield").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))); 
        break; 
        // dest1 = new LatLng(19.115177, 72.909486); 
      } 

      // Showing the current location in Google Map 
      map.moveCamera(CameraUpdateFactory.newLatLng(dest2)); 

      // Zoom in the Google Map 
      map.animateCamera(CameraUpdateFactory.zoomTo(15));  

      marker2.showInfoWindow(); 


      if(location!=null){ 
       onLocationChanged(location); 

      String url = getDirectionsUrl(origin, dest2);    

      DownloadTask downloadTask = new DownloadTask(); 

      // Start downloading json data from Google Directions API 
      downloadTask.execute(url); 
      } 
      } 
      catch(Exception e){ 

      } 
     }  
    } 
    /** A class to parse the Google Places in JSON format */ 
    private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String,String>>> >{ 

     // Parsing the data in non-ui thread   
     @Override 
     protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) { 

      JSONObject jObject; 
      List<List<HashMap<String, String>>> routes = null;      

      try{ 
       jObject = new JSONObject(jsonData[0]); 
       DirectionsJSONParser parser = new DirectionsJSONParser(); 

       // Starts parsing data 
       routes = parser.parse(jObject);  
      }catch(Exception e){ 
       e.printStackTrace(); 
      } 
      return routes; 
     } 

     // Executes in UI thread, after the parsing process 
     @Override 
     protected void onPostExecute(List<List<HashMap<String, String>>> result) { 
      ArrayList<LatLng> points = null; 
      //PolylineOptions lineOptions = null; 

      MarkerOptions markerOptions = new MarkerOptions(); 

      // Traversing through all the routes 
      for(int i=0;i<result.size();i++){ 
       points = new ArrayList<LatLng>(); 
       //lineOptions = new PolylineOptions();    

       // Fetching i-th route 
       List<HashMap<String, String>> path = result.get(i); 

       // Fetching all the points in i-th route 
       for(int j=0;j<path.size();j++){ 
        HashMap<String,String> point = path.get(j);     

        double lat = Double.parseDouble(point.get("lat")); 
        double lng = Double.parseDouble(point.get("lng")); 
        LatLng position = new LatLng(lat, lng); 

        points.add(position);      
       } 

       // Adding all the points in the route to LineOptions 
       lineOptions.addAll(points); 
       lineOptions.width(4); 
       lineOptions.color(Color.BLUE); 

      } 

      // Drawing polyline in the Google Map for the i-th route 
      //map.addPolyline(lineOptions); 
      line = map.addPolyline(lineOptions); 
     }   
    } 
} 
+0

請描述你的問題 – Xcihnegn 2015-02-23 08:59:07

+0

@Xingchen我顯示谷歌地圖與標記和折線它工作正常到Android 4.4,但Android 5.0聚線不來。多行意味着,例如,我想從當前位置(gps)到目的地位置繪製線,因爲我使用多行線,這是工作達4.4,但不工作在5.0這是我的問題 – 2015-02-23 10:33:52

+0

你的意思是折線不顯示沒有任何錯誤? – Xcihnegn 2015-02-23 10:36:45

回答

1

嘗試修改後的代碼爲您onPostExecute

@Override 
protected void onPostExecute(List<List<HashMap<String, String>>> result) { 
     ArrayList<LatLng> points = null; 
     //PolylineOptions lineOptions = null; 

     //MarkerOptions markerOptions = new MarkerOptions(); 

     points = new ArrayList<LatLng>(); 

     // Traversing through all the routes 
     for(int i=0;i<result.size();i++){ 
      //points = new ArrayList<LatLng>(); 
      //lineOptions = new PolylineOptions();    

      // Fetching i-th route 
      List<HashMap<String, String>> path = result.get(i); 

      // Fetching all the points in i-th route 
      for(int j=0;j<path.size();j++){ 
       HashMap<String,String> point = path.get(j);     

       double lat = Double.parseDouble(point.get("lat")); 
       double lng = Double.parseDouble(point.get("lng")); 
       LatLng position = new LatLng(lat, lng); 

       points.add(position);      
      } 

      // Adding all the points in the route to LineOptions 
      //lineOptions.addAll(points); 
      //lineOptions.width(4); 
      //lineOptions.color(Color.BLUE); 

     } 

     // Drawing polyline in the Google Map for the i-th route 
     //map.addPolyline(lineOptions); 

     lineOptions.addAll(points); 
     lineOptions.width(4); 
     lineOptions.color(Color.BLUE); 
     line = map.addPolyline(lineOptions); 
    }   

我之前points例如for循環動了你的列表,並且還在for循環之後的lineOptions

希望得到這個幫助。

+0

雅這是工作很好,非常感謝,但請你解釋這個問題的原因。我需要知道爲什麼以前它不工作,現在它是如何工作的 – 2015-02-23 12:50:50

0

現在Google又發佈了一個新API,但他們沒有正確更新文檔。要連接到API,您需要創建一個Google Play服務API客戶端實例。

mGoogleApiClient = new GoogleApiClient.Builder(this) 
    .addConnectionCallbacks(this) 
    .addOnConnectionFailedListener(this) 
    .addApi(LocationServices.API) 
    .build(); 

連接到Google Play服務和位置服務API後,您可以獲取用戶設備的最後一個已知位置。當您的應用程序連接到這些應用程序時,您可以使用融合位置提供程序的getLastLocation()方法來檢索設備位置。要做到這一點,你必須在你的類/活動

implements 
    ConnectionCallbacks, OnConnectionFailedListener 

然後實現這個接口,你可以發現這個實現的方法

@Override 
    public void onConnected(Bundle bundle) { 
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
       mGoogleApiClient); 
     if (mLastLocation != null) { 
      mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude())); 
      mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude())); 
     } 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 

    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 

    } 

這裏是我的完整code into this thread 。文檔is here

相關問題