1

我有兩個標記,分別是startLocation和另一個是stopLocation。 startLocation會檢測用戶的當前位置,然後用戶將走路,當他們停下來時,他們會按停止,並且將捕獲stopLocation作爲他們新的當前位置。我想繪製一條折線,因爲用戶正在從startLocation移動到stopLocation。 或者,也可以在兩個用於啓動和停止位置的標記已創建之後繪製多段線 - 以較高的可實施性爲準。 這怎麼辦?大部分答案都是指檢索路線然後繪製多段線,但這不是我想要的 - 我想要獲取用戶的個性化路線。總之,我想記錄用戶採取的路線。我已經成功地創建已有兩個標記:兩個標記之間的繪製路徑Google Maps V2

btnStart = (Button) findViewById(R.id.btnStart); 
    btnStart.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      // Create Start Marker 
      // get current location 
      LocationManager locManager; 
      String context = Context.LOCATION_SERVICE; 
      locManager = (LocationManager) getSystemService(context); 

      Criteria c = new Criteria(); 
      c.setAccuracy(Criteria.ACCURACY_FINE); 
      c.setAltitudeRequired(false); 
      c.setBearingRequired(false); 
      c.setCostAllowed(true); 
      c.setPowerRequirement(Criteria.POWER_LOW); 

      String provider = locManager.getBestProvider(c, true); 
      Location loc = locManager.getLastKnownLocation(provider); 

      LatLng currentPosition = updateWithNewLocation(loc); 

      Marker startLocation = map.addMarker(new MarkerOptions() 
        .position(currentPosition) 
        .title("Start Location") 
        .icon(BitmapDescriptorFactory 
          .defaultMarker(BitmapDescriptorFactory.HUE_AZURE))); 

      map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentPosition, 17)); 


     } 

    }); 

    btnStop = (Button) findViewById(R.id.btnStop); 
    btnStop.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      // Create Stop 

      // get current location 
      LocationManager locManager; 
      String context = Context.LOCATION_SERVICE; 
      locManager = (LocationManager) getSystemService(context); 

      Criteria c = new Criteria(); 
      c.setAccuracy(Criteria.ACCURACY_FINE); 
      c.setAltitudeRequired(false); 
      c.setBearingRequired(false); 
      c.setCostAllowed(true); 
      c.setPowerRequirement(Criteria.POWER_LOW); 

      String provider = locManager.getBestProvider(c, true); 
      Location loc = locManager.getLastKnownLocation(provider); 

      LatLng currentPosition = updateWithNewLocation(loc); 

      Marker stopLocation = map.addMarker(new MarkerOptions() 
        .position(currentPosition) 
        .title("Stop Location") 
        .icon(BitmapDescriptorFactory 
          .defaultMarker(BitmapDescriptorFactory.HUE_ORANGE))); 

      map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentPosition, 17)); 

      // Draw dynamic line 
     } 

    }); 

現在我需要的是繪製兩個標記之間的界線。謝謝!

回答

1

沒有跟蹤用戶的位置,沒有辦法做到這一點。您應該使用requestLocationUpdates函數來聆聽並獲取用戶位置的更新。有關收聽GPS位置的更多信息,請參閱the developer guide

String locationProvider = LocationManager.NETWORK_PROVIDER; 
// Or, use GPS location data: 
// String locationProvider = LocationManager.GPS_PROVIDER; 

locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener); 

您還可能要使用管理單元道路功能在newly released Google Maps Road API,以解決您的原始緯度/來自GPS LNG,並獲得道路上的平滑路徑。它目前沒有Android API,因此您可能需要使用Web API來訪問管理服務。

https://roads.googleapis.com/v1/snapToRoads?path=-35.27801,149.12958|-35.28032,149.12907|-35.28099,149.12929|-35.28144,149.12984|-35.28194,149.13003|-35.28282,149.12956|-35.28302,149.12881|-35.28473,149.12836 
     &interpolate=true 
     &key=API_KEY 

後用戶停止追蹤或到達終點,您可以根據自己的路徑上create a polyline

// Instantiates a new Polyline object and adds points to define a rectangle 
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); 

讓我知道,如果它是不明確的,希望它有助於。

+0

嘿,我會先試試這個!非常感謝你:-)真的很感激它! – user3763216

相關問題