2017-11-24 18 views
0

Googleマップを使用して簡単なナビゲーションアプリを実装しようとしています。最初は地図上に2つのジオ点をマーキングし、それらの間のルートを表示するコードを開発しました。今私は、ユーザーの場所に基づいて、2番目のマーカー(目的地)に向かって最初のマーカーを移動しようとしています。 私の最初のマーカはデバイスの場所で、2番目のマーカはapiを通してdbから取得されます。Androidで場所が変更されたときにマーカーを継続的に更新していません -

これに対して、私はLocationListenerを使用し、onLocationChangedにコードを実装しました。問題は、onLocationChangedが発生しておらず、マーカーがユーザーの場所に基づいて移動していないことです.Markerは毎回アプリを読み込む際にのみ移動します。 ここに私のコードです。

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

private GoogleMap mMap; 
ArrayList<LatLng> MarkerPoints = new ArrayList<>(); 
GoogleApiClient mGoogleApiClient; 
Location mLastLocation; 
Marker mCurrLocationMarker1,mCurrLocationMarker2; 
LocationRequest mLocationRequest; 

FrameLayout mainFrameLayout; 
Snackbar snackbar; 

private static final int PERMISSIONS_REQUEST_READ_PHONE_STATE = 1; 
String stringIMEI = "",stringLatitudeOrigin = "",stringLongitudeOrigin = "", 
     stringLatitudeDest = "",stringLongitudeDest = "",stringCurrentDate = ""; 
SharedPreferences sharedPreferences; 

@TargetApi(Build.VERSION_CODES.N) 
@RequiresApi(api = Build.VERSION_CODES.M) 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 

    sharedPreferences = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE); 
    Calendar c = Calendar.getInstance(); 
    System.out.println("Current time =&gt; "+c.getTime()); 

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    stringCurrentDate = df.format(c.getTime()); 
    // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(map); 
    mapFragment.getMapAsync(this); 

    mainFrameLayout = (FrameLayout)findViewById(R.id.mainFrameLayout); 

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     checkLocationPermission(); 
    } 

    if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.READ_PHONE_STATE) 
      != PackageManager.PERMISSION_GRANTED) { 
     requestPermissions(new String[]{android.Manifest.permission.READ_PHONE_STATE}, 
       PERMISSIONS_REQUEST_READ_PHONE_STATE); 
    }else { 
     TelephonyManager mngr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
     stringIMEI = mngr.getDeviceId().toString(); 
    } 
} 
@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.ACCESS_FINE_LOCATION) 
       == PackageManager.PERMISSION_GRANTED) { 
      buildGoogleApiClient(); 
      mMap.setMyLocationEnabled(true); 
     } 
    } 
    else { 
     buildGoogleApiClient(); 
     mMap.setMyLocationEnabled(true); 
    } 
    if (checkLocationPermission()) { 
     mMap.setMyLocationEnabled(true); 
     mMap.setTrafficEnabled(false); 
     mMap.setIndoorEnabled(false); 
     mMap.setBuildingsEnabled(true); 
     mMap.getUiSettings().setZoomControlsEnabled(true); 
     mMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder() 
       .target(mMap.getCameraPosition().target).zoom(17).bearing(30).tilt(45).build())); 
    } 
} 
private String getUrl(LatLng origin, LatLng dest) { 
    String str_origin = "origin=" + origin.latitude + "," + origin.longitude; 
    String str_dest = "destination=" + dest.latitude + "," + dest.longitude; 
    String key = "key=AIzaSyDkuXKvLCxtKs1jkXRXyt5Kk1Qv3fUe7mU"; 
    String parameters = str_origin + "&" + str_dest + "&" + key;//+ waypoints + "&" 
    String output = "json"; 
    String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters; 
    return url; 
} 
private class FetchUrl extends AsyncTask<String, Void, String> { 
    @Override 
    protected String doInBackground(String... url) { 
     // TODO Auto-generated method stub 
     String data = ""; 
     try { 
      HttpConnection http = new HttpConnection(); 
      data = http.downloadUrl(url[0]); 
     } catch (Exception e) { 
      // TODO: handle exception 
      Log.d("Background Task", e.toString()); 
     } 
     return data; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
     new ParserTask().execute(result); 
    } 
} 
private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String, String>>>> { 
    // Parsing the data in non-ui thread 
    @Override 
    protected List<List<HashMap<String, String>>> doInBackground(
      String... jsonData) { 
     // TODO Auto-generated method stub 
     JSONObject jObject; 
     List<List<HashMap<String, String>>> routes = null; 
     try { 
      jObject = new JSONObject(jsonData[0]); 
      if (!jObject.equals("")){ 
       //save route api call 
       SaveRouteVolleyRequest(jObject.toString()); 
       } 
      DirectionsJSONParser parser = new DirectionsJSONParser(); 
      routes = parser.parse(jObject); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return routes; 
    } 
    // Executes in UI thread, after the parsing process 
    @Override 
    protected void onPostExecute(List<List<HashMap<String, String>>> result) { 
     ArrayList<LatLng> points = new ArrayList<LatLng>(); 
     PolylineOptions lineOptions = new PolylineOptions(); 
     lineOptions.width(8); 
     lineOptions.color(Color.RED); 
     MarkerOptions markerOptions = new MarkerOptions(); 
     // Traversing through all the routes 
     for(int i=0;i<result.size();i++){ 
      // Fetching i-th route 
      List<HashMap<String, String>> path = result.get(i); 
      // Fetching all the points in i-th route 
      for(int j=0;j<path.size();j++){ 
       HashMap<String,String> point = path.get(j); 
       double lat = Double.parseDouble(point.get("lat")); 
       double lng = Double.parseDouble(point.get("lng")); 
       LatLng position = new LatLng(lat, lng); 
       points.add(position); 
      } 
      // Adding all the points in the route to LineOptions 
      lineOptions.addAll(points); 

     } 
     // Drawing polyline in the Google Map for the i-th route 
     if(points.size()!=0)mMap.addPolyline(lineOptions); 
    } 
} 

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

@Override 
public void onConnected(Bundle bundle) { 
    mLocationRequest = new LocationRequest(); 
    mLocationRequest.setInterval(1000); 
    mLocationRequest.setExpirationDuration(1000); 
    mLocationRequest.setFastestInterval(1000); 
    mLocationRequest.setSmallestDisplacement(0.1f); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    if (ContextCompat.checkSelfPermission(this, 
      Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
    } 
} 
@Override 
public void onConnectionSuspended(int i) { } 

@Override 
public void onLocationChanged(Location location) { 
    mLastLocation = location; 
    mMap.clear(); 
    if (mCurrLocationMarker1 != null) { 
     mCurrLocationMarker1.remove(); 
    } 
    //Place current location marker 
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
    stringLatitudeOrigin = String.valueOf(location.getLatitude()); 
    stringLongitudeOrigin = String.valueOf(location.getLongitude()); 
    mCurrLocationMarker1 = mMap.addMarker(new MarkerOptions().position(latLng).title("Your device").draggable(true) 
      .visible(true).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)).anchor(0.5f, 0.5f)); 

    if (isConnectingToInternet(MainActivity.this) == true) { 
     GetLocationVolleyRequest(); 
    }else{snackbar = Snackbar.make(mainFrameLayout, "No Internet Connection,Please Verify", Snackbar.LENGTH_LONG); 
     snackbar.show(); 
    } 
    //move map camera 
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,13.0f)); 
    //stop location updates 
    if (mGoogleApiClient != null) { 
     LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
    } 
} 
@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { } 

public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99; 
public boolean checkLocationPermission(){ 
    if (ContextCompat.checkSelfPermission(this, 
      Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
       Manifest.permission.ACCESS_FINE_LOCATION)) { 
      ActivityCompat.requestPermissions(this, 
        new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
        MY_PERMISSIONS_REQUEST_LOCATION); 
     } else { 
      ActivityCompat.requestPermissions(this, 
        new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
        MY_PERMISSIONS_REQUEST_LOCATION); 
     } 
     return false; 
    } else { 
     return true; 
    } 
} 
@Override 
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case MY_PERMISSIONS_REQUEST_LOCATION: { 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       if (ContextCompat.checkSelfPermission(this, 
         Manifest.permission.ACCESS_FINE_LOCATION) 
         == PackageManager.PERMISSION_GRANTED) { 
        if (mGoogleApiClient == null) { 
         buildGoogleApiClient(); 
        } 
        mMap.setMyLocationEnabled(true); 
        mMap.setTrafficEnabled(false); 
        mMap.setIndoorEnabled(false); 
        mMap.setBuildingsEnabled(true); 
        mMap.getUiSettings().setZoomControlsEnabled(true); 
        mMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder() 
          .target(mMap.getCameraPosition().target).zoom(17).bearing(30).tilt(45).build())); 
       } 
      }else { 
       snackbar = Snackbar.make(mainFrameLayout, "permission denied", Snackbar.LENGTH_LONG); 
       snackbar.show(); 
      } 
      return; 
     } 
     case PERMISSIONS_REQUEST_READ_PHONE_STATE : { 
      if (requestCode == PERMISSIONS_REQUEST_READ_PHONE_STATE 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       TelephonyManager mngr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
       stringIMEI = mngr.getDeviceId().toString(); 
      }else { 
       snackbar = Snackbar.make(mainFrameLayout, "permission denied", Snackbar.LENGTH_LONG); 
       snackbar.show(); 
      } 
     } 
    } 
} 
public void GetLocationVolleyRequest(){ 
    StringRequest stringRequest = new StringRequest(Request.Method.POST, Constants.LOCATIONAPI_URL, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        try { 
         JSONObject json = new JSONObject(response); 
         if (json.getString("success").equals("1")) { 
          if (json.has("id")) { 
           SharedPreferences.Editor editor = sharedPreferences.edit(); 
           editor.putString("device_id", json.getString("id")); 
           editor.commit(); 
          }else { 
           snackbar = Snackbar.make(mainFrameLayout, json.getString("msg"), Snackbar.LENGTH_LONG); 
           snackbar.getView().setBackgroundColor(R.color.colorPrimaryDark); 
           TextView mainTextView = (TextView) (snackbar.getView()).findViewById(android.support.design.R.id.snackbar_text); 
           mainTextView.setTextColor(Color.WHITE); 
           snackbar.show(); 
          } 
          if (json.has("latitude")) { 
           stringLatitudeDest = json.getString("latitude"); 
           stringLongitudeDest = json.getString("longitude"); 

           snackbar = Snackbar.make(mainFrameLayout, "Other Device -> Lat : " + stringLatitudeDest + " Lng : " + 
               stringLongitudeDest, Snackbar.LENGTH_LONG); 
           snackbar.getView().setBackgroundColor(R.color.colorPrimaryDark); 
           TextView mainTextView = (TextView) (snackbar.getView()).findViewById(android.support.design.R.id.snackbar_text); 
           mainTextView.setTextColor(Color.WHITE); 
           snackbar.show(); 

           mCurrLocationMarker2 = mMap.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(stringLatitudeDest), 
             Double.parseDouble(stringLongitudeDest))).title("Other device").icon(BitmapDescriptorFactory.fromResource(R.drawable.marker))); 

           MarkerPoints.add(new LatLng(Double.parseDouble(stringLatitudeOrigin), Double.parseDouble(stringLongitudeOrigin))); 
           MarkerPoints.add(new LatLng(Double.parseDouble(stringLatitudeDest), Double.parseDouble(stringLongitudeDest))); 

           if (MarkerPoints.size() >= 2) { 
            LatLng origin = MarkerPoints.get(0); 
            LatLng dest = MarkerPoints.get(1); 
            // Getting URL to the Google Directions API 
            String url = getUrl(origin, dest); 
            Log.d("onMapClick", url.toString()); 
            FetchUrl fetchUrl = new FetchUrl(); 
            // Start downloading json data from Google Directions API 
            fetchUrl.execute(url); 
            //move map camera 
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(origin, 14.0f)); 
           } 
          } 
         }else { 
          snackbar = Snackbar.make(mainFrameLayout, "No Co-ordinates due to"+json.getString("msg"), Snackbar.LENGTH_LONG); 
          snackbar.getView().setBackgroundColor(R.color.colorPrimaryDark); 
          TextView mainTextView = (TextView) (snackbar.getView()).findViewById(android.support.design.R.id.snackbar_text); 
          mainTextView.setTextColor(Color.WHITE); 
          snackbar.show(); 
         } 
        }catch (JSONException je){ 
         je.printStackTrace(); 
        } 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        snackbar = Snackbar.make(mainFrameLayout, error.toString(), Snackbar.LENGTH_LONG); 
        snackbar.getView().setBackgroundColor(R.color.colorPrimaryDark); 
        TextView mainTextView = (TextView) (snackbar.getView()).findViewById(android.support.design.R.id.snackbar_text); 
        mainTextView.setTextColor(Color.WHITE); 
        snackbar.show(); 
       } 
      }){ 
     @Override 
     protected Map<String,String> getParams(){ 
      String stringDeviceModel = Build.MODEL; 
      String stringDeviceSeries = Build.SERIAL; 
      String stringDeviceOS = Build.VERSION.RELEASE; 

      Map<String,String> params = new HashMap<String, String>(); 
      params.put("imei",stringIMEI); 
      params.put("model",stringDeviceModel); 
      params.put("series",stringDeviceSeries); 
      params.put("os",stringDeviceOS); 
      params.put("latitude",stringLatitudeOrigin); 
      params.put("longitude",stringLongitudeOrigin); 
      params.put("datetime",stringCurrentDate); 
      return params; 
     } 
    }; 
    RequestQueue requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(stringRequest); 
} 

public void SaveRouteVolleyRequest(String route){ 
    final String paramRoute = route; 
    StringRequest stringRequest = new StringRequest(Request.Method.POST, Constants.SAVEROUTEAPI_URL, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        try { 
         JSONObject json = new JSONObject(response); 
         if (json.getString("success").equals("1")) { 
          snackbar = Snackbar.make(mainFrameLayout, json.getString("msg"), Snackbar.LENGTH_LONG); 
          snackbar.getView().setBackgroundColor(R.color.colorPrimaryDark); 
          TextView mainTextView = (TextView) (snackbar.getView()).findViewById(android.support.design.R.id.snackbar_text); 
          mainTextView.setTextColor(Color.WHITE); 
          snackbar.show(); 
         }else { 
          snackbar = Snackbar.make(mainFrameLayout,"No Co-ordinates due to"+json.getString("msg"), Snackbar.LENGTH_LONG); 
          snackbar.getView().setBackgroundColor(R.color.colorPrimaryDark); 
          TextView mainTextView = (TextView) (snackbar.getView()).findViewById(android.support.design.R.id.snackbar_text); 
          mainTextView.setTextColor(Color.WHITE); 
          snackbar.show(); 
         } 
        }catch (JSONException je){ 
         je.printStackTrace(); 
        } 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        snackbar = Snackbar.make(mainFrameLayout, error.toString(), Snackbar.LENGTH_LONG); 
        snackbar.getView().setBackgroundColor(R.color.colorPrimaryDark); 
        TextView mainTextView = (TextView) (snackbar.getView()).findViewById(android.support.design.R.id.snackbar_text); 
        mainTextView.setTextColor(Color.WHITE); 
        snackbar.show(); 
       } 
      }){ 
     @Override 
     protected Map<String,String> getParams(){ 
      Map<String,String> params = new HashMap<String, String>(); 
      params.put("device_id",sharedPreferences.getString("device_id","")); 
      params.put("route",paramRoute); 
      return params; 
     } 
    }; 
    RequestQueue requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(stringRequest); 
} 

public boolean isConnectingToInternet(Context context){ 
    ConnectivityManager connectivity=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    if (connectivity != null) 
    { 
     NetworkInfo info[]=connectivity.getAllNetworkInfo(); 
     if(info!=null) 
     { 
      for(int i=0;i<info.length;i++) 
       if(info[i].getState()== NetworkInfo.State.CONNECTED) 
        return true; 
     } 
    } 
    return false; 
} 

}

答えて

0

問題は、あなたのonLocationChanged方法で

if (mGoogleApiClient != null) { 
     LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
    } 

です。 mGoogleApiClient != nullがtrueであるため、最初のロケーションアップデートを取得した瞬間にロケーションアップデートが削除されます。 removeLocationUpdatesをブロックonDestroyに移動するとすべてが機能します。

+0

入力していただきありがとうございます。 – Shivani

関連する問題