2013-10-12 2 views
0

내 코드가 작동하지 않는 이유를 모르겠다 ... 내 CurrentPosition을 연속적으로 유지하고 폴리선을 그려주는 ListArray를 만들었습니다. 여기 내 코드가있다.Google지도 Android Api V2 운전 경로가 작동하지 않음

MainActivity.java

내가 그것을 실행에는 어떤 경로를 보여주는이없는
package com.pondys.limon.touregsys; 

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

import com.google.android.gms.maps.CameraUpdate; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.Polyline; 
import com.google.android.gms.maps.model.PolylineOptions; 

import android.graphics.Color; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.provider.SyncStateContract.Constants; 
import android.support.v4.app.FragmentActivity; 
import android.view.Menu; 
import android.widget.Toast; 



public class MainActivity extends FragmentActivity implements LocationListener { 
    GoogleMap googleMap; 
    double latitude,longitude,firstLatitude,firstLongitude; 
    private ArrayList<LatLng> arrayPoints = null; 
    PolylineOptions polylineOptions; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

    // Getting reference to the SupportMapFragment of activity_main.xml 
     SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
     // Getting GoogleMap object from the fragment 
     googleMap = fm.getMap(); 
     // Enabling MyLocation Layer of Google Map 
     googleMap.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); 

    // Getting latitude of the current location 
      firstLatitude = location.getLatitude(); 

      // Getting longitude of the current location 
      firstLongitude = location.getLongitude();  

     CameraUpdate center= 
       CameraUpdateFactory.newLatLng(new LatLng(latitude,longitude)); 
      CameraUpdate zoom=CameraUpdateFactory.zoomTo(15); 
      googleMap.moveCamera(center); 
      googleMap.animateCamera(zoom); 

    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    public void onLocationChanged(Location location) { 

     // Getting latitude of the current location 
     latitude = location.getLatitude(); 

     // Getting longitude of the current location 
     longitude = location.getLongitude(); 

     // Creating a LatLng object for the current location 
     LatLng latLng = new LatLng (latitude,longitude); 
     ArrayList<LatLng> list = new ArrayList<LatLng>(); 
     list.add(latLng); 

     // Showing the current location in Google Map 
     googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 

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

     PolylineOptions o = new PolylineOptions().width(3).color(0xFFEE8888); 
     for(int i = 0; i<list.size(); i++){ 
      o.add(new LatLng(latitude,longitude)); 
     } 

     googleMap.addPolyline(o); 

     } 

    @Override 
    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 
     Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 
     Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); 

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 
    } 

} 

...

답변

0

당신은 이런 식으로 할 필요가 :

Polyline pathOptions = googleMap.addPolyline(new PolylineOptions().add(new LatLng(source_latitude, source_longitude), new LatLng(destination_latitude, destination_longitude))); 

// Set the polyline's color to blue 
pathOptions.setColor(Color.BLUE); 

// Set the polyline's width 
pathOptions.setWidth(2);