2013-05-05 2 views
0

나는 사용자의 올바른 위치를 검색 할 수 있어야하는 안드로이드 응용 프로그램을 만들고 다른 레이아웃의 경로를 보여주기 위해 HashMap에 배치합니다.Google지도 v2에서 내 위치가 화면에 표시되지 않습니까?

어쨌든 나의 위치는 내 위치가 바뀌면 위치 관리자가 Google지도 위치를 변경하지 않고 그 위치가 올바른 위치로 변경되었음을 알 수 있지만 카메라가 멀리 이동하지 않아 내 위치가 화면 이미지의 중심, 왜 말해줘?

계획 내 정확한 위치 여기

위에 내가 나의 새로운 위치에 대한 내 오랜 위치에서 하나의 폴리 라인을 참조 mapso에 갔어요 경로,하지만 첫 번째 카메라의 움직임을 표시하는 것은 내 코드입니다 :

public class RunningActivity extends FragmentActivity implements LocationListener{ 

// GoogleMap for the screen 
private GoogleMap runningMap; 

private LocationManager lm; 

// stop watch 
private Chronometer mChronometer; 

private double longitude,latitude; 

Location lastLocationloc=null; 

// start & stop the time 
private long startTime,elapsedTime; 
// to be used in timer 
private boolean runningIsOn = false;  
private Thread timer; 

Button start,stop; 
//saving position in a map 
Map<Integer, RouteObject> map = new HashMap<Integer, RouteObject>(); 

@Override 
protected void onCreate(Bundle savedInstanceState) {       
    super.onCreate(savedInstanceState); 

    if(isGooglePlay()){ 

     setContentView(R.layout.running); 
     setupMap(); 
    } 

    // Connect the chronometer to show the user his stopwatch     
    mChronometer = (Chronometer) findViewById(R.id.chronometer_running); 

    // Watch for button clicks. 
    start = (Button)findViewById(R.id.startRunningBtn); 
    start.setOnClickListener(mStartListener); 

    stop = (Button)findViewById(R.id.stopRunningBtn); 
    stop.setOnClickListener(mStopListener); 

    runningMap.setMyLocationEnabled(true); 
    runningMap.setOnMapLongClickListener(myOnMapLongClickListener); 

    timer = new Thread(){ 

     public void run(){ 

      try{ 

       int count = 1; 

       // While the user has not pressed the stop button 
       while(runningIsOn){ 

       // Sleeps for 5 seconds 
       sleep(5000); 

       // Put the object in the map 
       RouteObject object = new RouteObject(count,((double)System.nanoTime()/1000000000.0), longitude,latitude);     
       map.put(count,object); 

       // count the objects to give them a name. 
       count++; 

       }               
      } 

      catch(InterruptedException e){ 

       e.printStackTrace(); 
      } 

      // Write a test to the logcat 
      RouteObject.getMap(map);            
     }      
    };               
} 

OnMapLongClickListener myOnMapLongClickListener = new OnMapLongClickListener() { 

    @Override 
    public void onMapLongClick(LatLng point) { 
     runningMap.addMarker(new MarkerOptions().position(point).title(
       point.toString())); 

     Location myLocation = runningMap.getMyLocation(); 
     if (myLocation == null) { 
      Toast.makeText(getApplicationContext(), 
        "My location not available", Toast.LENGTH_LONG).show(); 
     } else { 
      PolylineOptions polylineOptions = new PolylineOptions(); 
      polylineOptions.add(point); 
      polylineOptions.add(new LatLng(myLocation.getLatitude(), 
        myLocation.getLongitude())); 
      runningMap.addPolyline(polylineOptions); 
     } 
    } 

}; 



View.OnClickListener mStartListener = new OnClickListener() { 
    public void onClick(View v) { 

     // Starting the chronometer to be shown on user screen 
     mChronometer.setBase(SystemClock.elapsedRealtime()); 
     mChronometer.start(); 
     // Starting the real timing to be used in database 
     startTime = System.nanoTime(); 
     // Setting value, to be used in thread 
     runningIsOn = true; 
     // Starting the Thread 
     timer.start(); 

     stop.setBackgroundResource(R.drawable.stopon); 
     start.setBackgroundResource(R.drawable.recordpause);   
    } 
}; 

View.OnClickListener mStopListener = new OnClickListener() { 

    public void onClick(View v) { 

     // thread should now be stopped 
     runningIsOn = false; 

     mChronometer.stop(); 

     stop.setBackgroundResource(R.drawable.stopoff); 
     start.setBackgroundResource(R.drawable.recordon); 

     elapsedTime = System.nanoTime() - startTime; 

     // get the seconds 
     double seconds = (double)elapsedTime/1000000000.0; 

     // Format the result with 4 decimals 
     DecimalFormat df = new DecimalFormat("0.0000"); 

     // To the screen 
     System.out.println("Elapsed time: "+ df.format(seconds)); 
     // To the database 
     System.out.println("Elapsed time: "+ seconds); 

     Toast.makeText(RunningActivity.this,df.format(seconds), Toast.LENGTH_LONG).show(); 


     Gson gson = new Gson(); 
     String list = gson.toJson(map); 
     Intent intent = new Intent(RunningActivity.this, RoutesActivity.class); 
     intent.putExtra("your_map", list); 
     startActivity(intent); 

    } 
}; 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    if(item.getItemId() == R.id.action_legalnotices){ 

     startActivity(new Intent(this,LegalNoticesActivity.class)); 
    } 
    return super.onOptionsItemSelected(item); 
} 

@Override 
public void onLocationChanged(Location location) { 

    runningMap.setMapType(4); 

    if(lastLocationloc == null){ 

     lastLocationloc = location; 
    } 

    LatLng lastLatLng= locationToLatLng(lastLocationloc); 
    LatLng thisLatLng= locationToLatLng(location); 

    PolylineOptions polylineOptions = new PolylineOptions(); 
    polylineOptions.add(thisLatLng); 
    polylineOptions.add(lastLatLng); 
    runningMap.addPolyline(polylineOptions); 

    lastLocationloc = location; 

    LatLng latlng = new LatLng(location.getLatitude(),location.getLongitude()); 

    CameraUpdate center=CameraUpdateFactory.newLatLng(latlng); 
    CameraUpdate zoom=CameraUpdateFactory.zoomTo(17); 

    runningMap.moveCamera(center); 
    runningMap.animateCamera(zoom); 

    String message = String.format("Your new location found at: \n Longitude: %1$s \n Latitude: %2$s",location.getLongitude(), location.getLatitude());   
    Toast.makeText(RunningActivity.this, message, Toast.LENGTH_LONG).show(); 

    lm.removeUpdates(this); 



} 

@Override 
public void onProviderDisabled(String provider) { 
    // TODO Auto-generated method stub 
    Toast.makeText(RunningActivity.this,"Provider disabled by the user. GPS turned off",Toast.LENGTH_LONG).show(); 
} 

@Override 
public void onProviderEnabled(String provider) { 
    // TODO Auto-generated method stub 
    Toast.makeText(RunningActivity.this,"Provider enabled by the user. GPS turned on",Toast.LENGTH_LONG).show(); 
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
    // TODO Auto-generated method stub 
    Toast.makeText(RunningActivity.this,"Provider status changed",Toast.LENGTH_LONG).show(); 
} 

private void setupMap() { 

    if (runningMap == null) { 

     runningMap = ((SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.running_map)).getMap(); 

     if (runningMap != null) { 

      runningMap.setMyLocationEnabled(true); 
      runningMap.animateCamera(CameraUpdateFactory.zoomBy(17)); 

      lm = (LocationManager) getSystemService(LOCATION_SERVICE); 

      String provider = lm.getBestProvider(new Criteria(), true); 

      if (provider == null) { 

       // ask the user to turn on his gps 
       onProviderDisabled(provider); 
      } 

      Location loc = lm.getLastKnownLocation(provider); 

      if (loc != null) { 

       onLocationChanged(loc); 
      } 


     } 

    } 
} 

private boolean isGooglePlay(){ 

    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 

    if(status == ConnectionResult.SUCCESS){ 

     return(true); 
    }else{ 


     //Google Play services own error mesage 
     //((Dialog)GooglePlayServicesUtil.getErrorDialog(status,this, 10)).show(); 
     Toast.makeText(this,"Google Play is not available", Toast.LENGTH_SHORT).show();  
    } 

    return(false); 

} 

public static LatLng locationToLatLng(Location loc) { 
    if(loc != null) 
     return new LatLng(loc.getLatitude(), loc.getLongitude()); 
    return null; 
}  

}

+0

나는이 문제에 대한 해결책을 발견했다. 문제는 OnLocationChanged 클래스가 한 번 이상 실행되지 않는다는 것입니다. SDK에서 실패한 것입니다. – Friis1978

답변

0

이 솔루션은 여기에, 활동은 이제 startbutton을 누르면 새 위치에 마지막 위치에서 선을 기록합니다. 여기에 코드가 있습니다. 이 코드는 매 5 초마다 폴리 라인을 만듭니다. 귀하가 Google지도 응용 프로그램을 만드는 경우, 귀하가 그것을 사용할 수 있기를 희망합니다.

public class RunningActivity extends FragmentActivity implements LocationListener, OnMyLocationChangeListener { 
private GoogleMap map; 
private LocationManager locationManager; 

private static final long MIN_TIME = 5000; 
private static final float MIN_DISTANCE = 5; 

// stop watch 
private Chronometer mChronometer; 

private double longitude, latitude; 

Location lastLocationloc = null; 

// start & stop the time 
private long startTime, elapsedTime; 
// to be used in timer 
private boolean runningIsOn = false; 
private Thread timer; 

Button start, stop; 
// saving position in a map 
Map<Integer, RouteObject> hashmap = new HashMap<Integer, RouteObject>(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.running); 
    map = ((SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.running_map)).getMap(); 

    map.setMapType(4); 
    map.setMyLocationEnabled(true); 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    String provider = locationManager.getBestProvider(new Criteria(), true); 
    locationManager.requestLocationUpdates(provider, MIN_TIME, 
      MIN_DISTANCE, this); 

    timer = new Thread() { 

     public void run() { 

      try { 

       int count = 1; 

       // While the user has not pressed the stop button 
       while (runningIsOn) { 

        // Sleeps for 5 seconds 
        sleep(5000); 

        // Put the object in the map 
        RouteObject object = new RouteObject(count, 
          ((double) System.nanoTime()/1000000000.0), 
          longitude, latitude); 
        hashmap.put(count, object); 

        // count the objects to give them a name. 
        count++; 

       } 
      } 

      catch (InterruptedException e) { 

       e.printStackTrace(); 
      } 

      // Write a test to the logcat 
      RouteObject.getMap(hashmap); 
     } 
    }; 

    // Connect the chronometer to show the user his stopwatch 
    mChronometer = (Chronometer) findViewById(R.id.chronometer_running); 

    // Watch for button clicks. 
    start = (Button) findViewById(R.id.startRunningBtn); 
    start.setOnClickListener(mStartListener); 

    stop = (Button) findViewById(R.id.stopRunningBtn); 
    stop.setOnClickListener(mStopListener); 

    map.setOnMyLocationChangeListener(this); 
} 

View.OnClickListener mStartListener = new OnClickListener() { 
    public void onClick(View v) { 

     // Starting the chronometer to be shown on user screen 
     mChronometer.setBase(SystemClock.elapsedRealtime()); 
     mChronometer.start(); 
     // Starting the real timing to be used in database 
     startTime = System.nanoTime(); 
     // Setting value, to be used in thread 
     runningIsOn = true; 
     // Starting the Thread 
     timer.start(); 

     stop.setBackgroundResource(R.drawable.stopon); 
     start.setBackgroundResource(R.drawable.recordpause); 
    } 
}; 

View.OnClickListener mStopListener = new OnClickListener() { 

    public void onClick(View v) { 

     // thread should now be stopped 
     runningIsOn = false; 

     mChronometer.stop(); 

     stop.setBackgroundResource(R.drawable.stopoff); 
     start.setBackgroundResource(R.drawable.recordon); 

     elapsedTime = System.nanoTime() - startTime; 

     // get the seconds 
     double seconds = (double) elapsedTime/1000000000.0; 

     // Format the result with 4 decimals 
     DecimalFormat df = new DecimalFormat("0.0000"); 

     // To the screen 
     System.out.println("Elapsed time: " + df.format(seconds)); 
     // To the database 
     System.out.println("Elapsed time: " + seconds); 

     Toast.makeText(RunningActivity.this, df.format(seconds), 
       Toast.LENGTH_LONG).show(); 

    } 
}; 

@Override 
public void onMyLocationChange(Location lastKnownLocation) { 

    changeMapLocation(lastKnownLocation); 
} 

private void changeMapLocation(Location location) { 

    if (lastLocationloc == null) { 

     lastLocationloc = location; 
    } 

    LatLng lastLatLng = new LatLng(lastLocationloc.getLatitude(),lastLocationloc.getLongitude()); 
    LatLng thisLatLng = new LatLng(location.getLatitude(),location.getLongitude()); 

    // set variables for the hashmap 
    longitude = location.getLongitude(); 
    latitude = location.getLatitude(); 

    if(runningIsOn) { 

    map.addPolyline(new PolylineOptions() 
    .add(thisLatLng,lastLatLng) 
    .width(2) 
    .color(Color.RED)); 

    } 

    map.animateCamera(CameraUpdateFactory.newLatLngZoom(thisLatLng, 18),null); 

    String old_message = String.format("My Old location: \n Longitude: %1$s \n Latitude: %2$s",lastLocationloc.getLongitude(),lastLocationloc.getLatitude()); 
    Toast.makeText(RunningActivity.this, old_message,Toast.LENGTH_LONG).show(); 

    String message = String.format("My new location: \n Longitude: %1$s \n Latitude: %2$s",location.getLongitude(),location.getLatitude()); 
    Toast.makeText(RunningActivity.this, message,Toast.LENGTH_LONG).show(); 

    lastLocationloc = location; 

    locationManager.removeUpdates(this); 
} 

@Override 
public void onLocationChanged(Location location) { 

} 

@Override 
public void onProviderDisabled(String provider) { 
    // TODO Auto-generated method stub 
} 

@Override 
public void onProviderEnabled(String provider) { 
    // TODO Auto-generated method stub 

} 

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

} 

}

관련 문제