2011-06-09 56 views
1

在我的應用程序中,我在地圖2位置顯示,並用標記標記它們。現在,我想繪製他們之間的路線,我不知道我該怎麼做。我的功能畫圖應該如何?繪製2個覆蓋層之間的路線

這是我的代碼:

package com.ShoppingList.Maps; 

import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Point; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.KeyEvent; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.ShoppingList.R; 
import com.google.android.maps.GeoPoint; 
import com.google.android.maps.ItemizedOverlay; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapView; 
import com.google.android.maps.MyLocationOverlay; 
import com.google.android.maps.Overlay; 
import com.google.android.maps.OverlayItem; 
import com.google.android.maps.Projection; 

import java.util.ArrayList; 
import java.util.List; 

public class OnMap extends MapActivity { 
    private MapView map = null; 
    private MyLocationOverlay me = null; 
    //private myOverlay m = null; 

    double latitudine; 
    double longitudine; 

    double latshop; 
    double longshop; 

    String nameshop; 

    Canvas canvas = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.shopsonmap); 

     map = (MapView) findViewById(R.id.shopsonmap); 

     latitudine = getIntent().getDoubleExtra("latcurent", 0); 
     longitudine = getIntent().getDoubleExtra("longcurent", 0); 
     latshop = getIntent().getDoubleExtra("latshop", 0); 
     longshop = getIntent().getDoubleExtra("longshop", 0); 
     nameshop = getIntent().getStringExtra("nameshop"); 

     GeoPoint p1 = new GeoPoint((int) latitudine, (int) longitudine); 
     GeoPoint p2 = new GeoPoint((int) latshop, (int) longshop); 

     map.getController().setCenter(getPoint(latitudine, longitudine)); 
     map.getController().setZoom(15); 
     map.setBuiltInZoomControls(true); 
     map.setSatellite(false); 
     map.setStreetView(true); 
     map.invalidate(); 

     Drawable marker = getResources().getDrawable(R.drawable.marker); 

     marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker 
       .getIntrinsicHeight()); 

     map.getOverlays().add(new SitesOverlay(marker)); 

     me = new MyLocationOverlay(this, map); 
     map.getOverlays().add(me); 


    } 

    /*class myOverlay extends Overlay { 
     GeoPoint gp1; 
     GeoPoint gp2; 

     public myOverlay(GeoPoint gp1, GeoPoint gp2) { 

      this.gp1 = gp1; 
      this.gp2 = gp2; 

     } 

     public void draw(Canvas canvas, MapView mapView, boolean shadow) { 

      Projection projection = mapView.getProjection(); 
      Paint mPaint = new Paint(); 
      Point from = new Point(); 
      projection.toPixels(gp1, from); 
      mPaint.setColor(Color.BLUE); 

      Point to = new Point(); 
      projection.toPixels(gp2, to); 
      mPaint.setStrokeWidth(9); 
      mPaint.setAlpha(120); 
      canvas.drawLine(from.x, from.y, to.x, to.y, mPaint); 
      super.draw(canvas, mapView, shadow); 

     } 
    }*/ 

    @Override 
    public void onResume() { 
     super.onResume(); 

     me.enableCompass(); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 

     me.disableCompass(); 
    } 

    @Override 
    protected boolean isRouteDisplayed() { 
     return (false); 
    } 

    private GeoPoint getPoint(double lat, double lon) { 
     return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0))); 
    } 

    private class SitesOverlay extends ItemizedOverlay<OverlayItem> { 
     private List<OverlayItem> items = new ArrayList<OverlayItem>(); 
     private Drawable marker = null; 

     public SitesOverlay(Drawable marker) { 
      super(marker); 
      this.marker = marker; 

      items.add(new OverlayItem(getPoint(latitudine, longitudine), 
        "Your location", "You are here!")); 

      items.add(new OverlayItem(getPoint(latshop, longshop), "The shop", 
        "The shop " + nameshop + " is here")); 

      populate(); 
     } 

     @Override 
     protected OverlayItem createItem(int i) { 
      return (items.get(i)); 
     } 

     @Override 
     public void draw(Canvas canvas, MapView mapView, boolean shadow) { 

      super.draw(canvas, mapView, shadow); 

      boundCenterBottom(marker); 
     } 

     @Override 
     protected boolean onTap(int i) { 
      Toast.makeText(OnMap.this, items.get(i).getSnippet(), 
        Toast.LENGTH_SHORT).show(); 

      return (true); 
     } 

     @Override 
     public int size() { 
      return (items.size()); 
     } 
    } 
} 

謝謝..

回答

1

因此,讓我們假設您從REST Web服務獲取位置(使用JSON)。爲此,我使用Volley庫連接並獲取服務器的響應。 JSONArray響應的

實施例:

[{..., 「位置」: 「44.924654,8.586219」,...},{ ..., 「位置」:「44.906177,8.157752 「,...}, {...,」location「:」44.906177,8.157752「,...},{...,」location「: 」44.956733,7.876227「,...},{ ...,「位置」:「45.034424,7.671607」, ...}]

的步驟是設置在第一和作爲標記的最後位置,並且中間位置將畫線之間它們。

因爲位置是以字符串形式獲得的,所以我們首先分割字符串,並將「,」之前的部分指定爲緯度,其餘部分指定爲經度。

    public void onResponse(JSONArray response) { 

        if (response.length() > 0) { 
         try { 
          //creating the markers: for this I need the first and the last location 
          JSONObject firstLocationJson = response.getJSONObject(0); 
          JSONObject lastLocationJson = response.getJSONObject(response.length() - 1); 

          String[] firstLocationLatLng = firstLocationJson.getString("location").split(","); 
          Location firstLocation = new Location(LocationManager.GPS_PROVIDER); 
          firstLocation.setLatitude(Double.parseDouble(firstLocationLatLng[0])); 
          firstLocation.setLongitude(Double.parseDouble(firstLocationLatLng[1])); 

          String[] lastLocationLatLng = lastLocationJson.getString("location").split(","); 
          Location lastLocation = new Location(LocationManager.GPS_PROVIDER); 
          lastLocation.setLatitude(Double.parseDouble(lastLocationLatLng[0])); 
          lastLocation.setLongitude(Double.parseDouble(lastLocationLatLng[1])); 

          final float distance = firstLocation.distanceTo(lastLocation); //distance in meters 

          if (distance > 50000 && distance < 200000) { //distance bigger than 50 km 
           showMapView(response, firstLocation, lastLocation, 7); 
          } else if (distance > 300000) { 
           showMapView(response, firstLocation, lastLocation, 5); 
          } 
         } catch (JSONException e) { 
          // TODO 
         } 
        } 
        // TODO - 
       } 

現在讓我們來看看繪製我們的MapView的方法。請注意,我不在一個活動中,如果我想強制代碼在主線程上運行(用於更新UI),我將使用一個Handler和一個Runnable。 方法showMapView()是一個負責繪製標記和兩者之間的位置的人。

private void showMapView(JSONArray response, Location firstLoc, Location lastLoc, final int zoom) { 
    final LatLng latLng1 = new LatLng(firstLoc.getLatitude(), firstLoc.getLongitude()); 
    final LatLng latLng2 = new LatLng(lastLoc.getLatitude(), lastLoc.getLongitude()); 
    final MarkerOptions marker1 = new MarkerOptions().position(latLng1); 
    final MarkerOptions marker2 = new MarkerOptions().position(latLng2); 

    final PolylineOptions polylineOptions = new PolylineOptions(); 
    final ArrayList<LatLng> points = new ArrayList<LatLng>(); 
    //saving all the locations in an ArrayList 
    if (response.length() > 0) { 
     for (int i = 0; i < response.length(); i++) { 
      JSONObject locationsJson = null; 
      try { 
       locationsJson = response.getJSONObject(i); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      String locationString = null; 
      try { 
       locationString = locationsJson.getString("location"); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      //here I am splitting the location string in a String array. 
      String[] locationLatLng = locationString.split(","); 
      Location loc = new Location(LocationManager.GPS_PROVIDER); 
      loc.setLatitude(Double.parseDouble(locationLatLng[0])); 
      loc.setLongitude(Double.parseDouble(locationLatLng[1])); 
      LatLng latLng = new LatLng(loc.getLatitude(), loc.getLongitude()); 
      points.add(latLng); 

     } 
    } 

    Handler mainHandler = new Handler(Looper.getMainLooper()); 

    Runnable myRunnable = new Runnable() { 
     @Override 
     public void run() { 
      mapView.getMapAsync(new OnMapReadyCallback() { 
       @Override 
       public void onMapReady(GoogleMap googleMap) { 
        googleMap.addMarker(marker1); 
        googleMap.addMarker(marker2); 
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng1, zoom)); 
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng2, zoom)); 
        polylineOptions.addAll(points); 
        polylineOptions.width(10); 
        polylineOptions.color(Color.BLUE); 
        googleMap.addPolyline(polylineOptions); 
       } 
      }); 
     } 
    }; 

    mainHandler.post(myRunnable); 
} 

的代碼是清楚和明白,這些點(中間位置)使用類型的PolylineOptions的目的繪製和它正在使用這條線添加到地圖:googleMap.addPolyline(polylineOptions);

所需的縮放水平,是在2.0到21.0的範圍內。低於這個範圍的值被設置爲2.0,並且高於它的值被設置爲21.0。增加要放大的值。並非所有區域都具有最大縮放級別的圖塊。 read here about zoom

相關問題