2017-12-12 6 views
1

地図上に現在の更新された場所を表示しようとしています。更新された現在地を地図上に表示する方法Googleマップ

マップと位置をズームインするとズームアウトしてから位置を表示します。同じズーム位置に更新された位置は表示されません。 場所の更新時に地図上に再びマーカーを設定します。マップ上にマーカーを設定する際に問題があると思います。現在更新されている位置を 地図に表示してください。

public class MainActivity extends FragmentActivity implements OnMapReadyCallback , GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, LocationListener { 

GoogleMap googleMa; 
double latitude; 
private GoogleApiClient mGoogleApiClient; 
double longitude; 
private Location mLastLocation = null; 
private LocationRequest mLocationRequest; 
String mPermission = android.Manifest.permission.ACCESS_FINE_LOCATION; 
protected LocationManager locationManager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 
      2); 

    locationManager = (LocationManager) MainActivity.this 
      .getSystemService(Context.LOCATION_SERVICE); 
    boolean isGPSEnabled = locationManager 
      .isProviderEnabled(LocationManager.GPS_PROVIDER); 

    if (checkPlayServices()) { 

     buildGoogleApiClient(); 
     createLocationRequest(); 
     displayLocation(); 

    } 
    initailizeMap(); 
} 

public void initailizeMap() { 
    if (googleMa == null) { 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
    } 
} 

@Override 
public void onMapReady(GoogleMap googleMap) { 
    googleMa = googleMap; 
    Log.d("aaaaaa", " on map --->" + latitude + " " + longitude); 
    displayLocation(); 

} 

public void displayLocation() { 
    try { 
     GPSTracker gps = new GPSTracker(MainActivity.this); 
     if (gps.canGetLocation()) { 
      latitude = gps.getLatitude(); 
      longitude = gps.getLongitude(); 
      Toast.makeText(getApplicationContext(), "Latitude: " + latitude + " Longitude: " + longitude, Toast.LENGTH_LONG).show(); 
      final LatLng loc = new LatLng(latitude, longitude); 
      Marker ham = googleMa.addMarker(new MarkerOptions().position(loc).title("This is Me").icon(BitmapDescriptorFactory.fromResource(R.drawable.greenpointer))); 
      googleMa.moveCamera(CameraUpdateFactory.newLatLngZoom(loc, 15)); 
     } 
    } catch (Exception e) { 
    } 
} 

@Override 
public void onConnectionFailed(ConnectionResult result) { 
    Log.i("aaaaaaaa", "Connection failed: ConnectionResult.getErrorCode() = " 
      + result.getErrorCode()); 
} 

@Override 
public void onConnected(Bundle arg0) { 
    startLocationUpdates(); 
} 

@Override 
public void onConnectionSuspended(int arg0) { 
    mGoogleApiClient.connect(); 
} 

@Override 
public void onLocationChanged(Location location) { 
    mLastLocation = location; 
    Log.d("aaaaaaaa===>", "" + String.valueOf(location.getLatitude()) + "\n" + String.valueOf(location.getLongitude())); 
    Toast.makeText(getApplicationContext(), "Location changed!", 
      Toast.LENGTH_SHORT).show(); 
    displayLocation(); 
} 

private boolean checkPlayServices() { 

    GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance(); 

    int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(this); 

    if (resultCode != ConnectionResult.SUCCESS) { 
     if (googleApiAvailability.isUserResolvableError(resultCode)) { 
      googleApiAvailability.getErrorDialog(this, resultCode, 
        1000).show(); 
     } else { 
      Toast.makeText(getApplicationContext(), 
        "This device is not supported.", Toast.LENGTH_LONG) 
        .show(); 
      finish(); 
     } 
     return false; 
    } 
    return true; 
} 

protected synchronized void buildGoogleApiClient() { 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API).build(); 

    mGoogleApiClient.connect(); 

    LocationRequest mLocationRequest = new LocationRequest(); 
    mLocationRequest.setInterval(100); 
    mLocationRequest.setFastestInterval(500); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() 
      .addLocationRequest(mLocationRequest); 

    PendingResult<LocationSettingsResult> result = 
      LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build()); 

    result.setResultCallback(new ResultCallback<LocationSettingsResult>() { 
     @Override 
     public void onResult(LocationSettingsResult locationSettingsResult) { 

      final Status status = locationSettingsResult.getStatus(); 

      switch (status.getStatusCode()) { 
       case LocationSettingsStatusCodes.SUCCESS: 
        break; 
       case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: 
        try { 
         status.startResolutionForResult(MainActivity.this, 2000); 

        } catch (IntentSender.SendIntentException e) { 
        } 
        break; 
       case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: 
        break; 
      } 
     } 
    }); 
} 

protected void createLocationRequest() { 
    mLocationRequest = new LocationRequest(); 
    mLocationRequest.setInterval(10000); // 10 sec 
    mLocationRequest.setFastestInterval(5000); // 5 sec 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    mLocationRequest.setSmallestDisplacement(10); // 10 meters 
} 

protected void startLocationUpdates() { 
    LocationServices.FusedLocationApi.requestLocationUpdates(
      mGoogleApiClient, mLocationRequest, this); 
} 

protected void stopLocationUpdates() { 
    LocationServices.FusedLocationApi.removeLocationUpdates(
      mGoogleApiClient, this); 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    stopLocationUpdates(); 
} 

}

答えて

2

onLocationChangedがトリガされた後、あなたは(私は、GPSTrackerが何であるか全くコードを知っていませんが、確かに、それはあなたが更新され与えていないではないgpsからlocationインスタンスからlatitudelongitudeを取得する必要があります)、経度緯度

@Override 
public void onLocationChanged(Location location) { 
    mLastLocation = location; 
    // use latitude and longitude given by 
    // location.getLatitude(), location.getLongitude() 
    // for updated location marker 
    Log.d("aaaaaaaa===>", "" + location.getLatitude() + "\n" + location.getLongitude()); 
    // displayLocation(); 

    // to remove old markers 
    googleMa.clear(); 
    final LatLng loc = new LatLng(location.getLongitude(), location.getLongitude()); 

    Marker ham = googleMa.addMarker(new MarkerOptions().position(loc).title("This is Me").icon(BitmapDescriptorFactory.fromResource(R.drawable.greenpointer))); 
    googleMa.moveCamera(CameraUpdateFactory.newLatLngZoom(loc, 15)); 
} 
+0

私は場所にマーカーが機能を変更使用さが、私は、locationChangeコール –

+0

@DeepakShにする場合、複数のマーカーを取得しますarma oh、古いマーカーをクリアする必要があります。関数googleMa.clear()があると思います。マーカーを追加する前にこれを呼び出してください。 –

+0

ありがとうございます。うまく動作しています。 –

関連する問題