title: GoogleMaps displaying "locals" on wrong position
body: <p>i want to display the <code>CurrentPosition</code> of the mobile phone and display all <code>bar|cafe</code> nearby the position.</p>
  
  <p>The <code>CurrentPosition</code> works. </p>
  
  <p>But the displaying of the bars/cafes is wrong. It seems like they are showing up from the center of vienna and not from the position of my phone.</p>
  
  <p>Would be really thankful if someone could find the problem</p>
  
  <p>MapsActivity.java</p>
  
  <pre><code>package androfenix.currentpositionandplacesnearby;
  
  import android.Manifest;
  import android.content.pm.PackageManager;
  import android.location.Location;
  import android.os.AsyncTask;
  import android.os.Build;
  import android.support.v4.app.ActivityCompat;
  import android.os.Bundle;
  import android.support.v4.content.ContextCompat;
  import android.support.v7.app.AppCompatActivity;
  import android.util.Log;
  import android.widget.Toast;
  
  import com.google.android.gms.common.ConnectionResult;
  import com.google.android.gms.common.api.GoogleApiClient;
  import com.google.android.gms.location.LocationListener;
  import com.google.android.gms.location.LocationRequest;
  import com.google.android.gms.location.LocationServices;
  import com.google.android.gms.maps.CameraUpdateFactory;
  import com.google.android.gms.maps.GoogleMap;
  import com.google.android.gms.maps.OnMapReadyCallback;
  import com.google.android.gms.maps.SupportMapFragment;
  import com.google.android.gms.maps.model.BitmapDescriptorFactory;
  import com.google.android.gms.maps.model.LatLng;
  import com.google.android.gms.maps.model.Marker;
  import com.google.android.gms.maps.model.MarkerOptions;
  
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.net.HttpURLConnection;
  import java.net.URL;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  
  
  public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback,
          GoogleApiClient.ConnectionCallbacks,
          GoogleApiClient.OnConnectionFailedListener,
          LocationListener {
  
      private GoogleMap mMap;
      GoogleApiClient mGoogleApiClient;
      Location mLastLocation;
      Marker mCurrLocationMarker;
      LocationRequest mLocationRequest;
  
      LatLng latLng;
      double mLatitude=0;
      double mLongitude=0;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_maps);
  
          if (android.os.Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
              checkLocationPermission();
          }
          // Obtain the SupportMapFragment and get notified when the map is ready to be used.
          SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                  .findFragmentById(R.id.map);
          mapFragment.getMapAsync(this);
      }
  
      @Override
      public void onMapReady(GoogleMap googleMap) {
          mMap = googleMap;
  
          //Mit setMapType setzen wir das Aussehen der Karte auf "Hybrid"
          mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
  
          //Initialize Google Play Services
          if (android.os.Build.VERSION.SDK_INT &gt;= 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);
          }
  
      }
  
      protected synchronized void buildGoogleApiClient() {
          mGoogleApiClient = new GoogleApiClient.Builder(this)
                  .addConnectionCallbacks(this)
                  .addOnConnectionFailedListener(this)
                  .addApi(LocationServices.API)
                  .build();
  
          mGoogleApiClient.connect();
      }
  
      @Override
      public void onPause()
      {
          super.onPause();
          //Unregister for location callbacks:
          if (mGoogleApiClient != null)
          {
              LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
          }
      }
  
      @Override
      public void onConnected(Bundle bundle) {
  
          mLocationRequest = new LocationRequest();
          mLocationRequest.setInterval(1000);
          mLocationRequest.setFastestInterval(1000);
          mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_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;
          if (mCurrLocationMarker != null) {
              mCurrLocationMarker.remove();
          }
  
          // Create a LatLng object for the current location
          latLng = new LatLng(location.getLatitude(), location.getLongitude());
  
          mLatitude = location.getLatitude();
          mLongitude = location.getLongitude();
  
          //Place current location marker
          LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
          MarkerOptions markerOptions = new MarkerOptions();
          markerOptions.position(latLng);
          markerOptions.title("Current Position");
          markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
          mCurrLocationMarker = mMap.addMarker(markerOptions);
  
          //move map camera
          mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
          mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
  
          //stop location updates
          if (mGoogleApiClient != null) {
              LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
          }
  
          StringBuilder sbValue = new StringBuilder(sbMethod());
          PlacesTask placesTask = new PlacesTask();
          placesTask.execute(sbValue.toString());
      }
  
      @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) {
  
              // Asking user if explanation is needed
              if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                      Manifest.permission.ACCESS_FINE_LOCATION)) {
  
                  // Show an explanation to the user *asynchronously* -- don't block
                  // this thread waiting for the user's response! After the user
                  // sees the explanation, try again to request the permission.
  
                  //Prompt the user once explanation has been shown
                  ActivityCompat.requestPermissions(this,
                          new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                          MY_PERMISSIONS_REQUEST_LOCATION);
  
  
              } else {
                  // No explanation needed, we can request the permission.
                  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 request is cancelled, the result arrays are empty.
                  if (grantResults.length &gt; 0
                          &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  
                      // permission was granted. Do the
                      // contacts-related task you need to do.
                      if (ContextCompat.checkSelfPermission(this,
                              Manifest.permission.ACCESS_FINE_LOCATION)
                              == PackageManager.PERMISSION_GRANTED) {
  
                          if (mGoogleApiClient == null) {
                              buildGoogleApiClient();
                          }
                          mMap.setMyLocationEnabled(true);
                      }
  
                  } else {
  
                      // Permission denied, Disable the functionality that depends on this permission.
                      Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
                  }
                  return;
              }
  
              // other 'case' lines to check for other permissions this app might request.
              // You can add here other case statements according to your requirement.
          }
      }
  
      public StringBuilder sbMethod() throws SecurityException
      {
          StringBuilder sb = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
          sb.append("location=" + mLatitude + "," + mLongitude);
          sb.append("&amp;radius=50000");
          sb.append("&amp;sensor=true");
          sb.append("&amp;keyword=" + "bar|cafe");
          sb.append("&amp;key= SERVER API KEY ");
  
          Log.d("Map", "url: " + sb.toString());
  
          return sb;
      }
  
      private class PlacesTask extends AsyncTask&lt;String, Integer, String&gt;
      {
  
          String data = null;
  
          // Invoked by execute() method of this object
          @Override
          protected String doInBackground(String... url) {
              try {
                  data = downloadUrl(url[0]);
              } catch (Exception e) {
                  Log.d("Background Task", e.toString());
              }
              return data;
          }
  
          // Executed after the complete execution of doInBackground() method
          @Override
          protected void onPostExecute(String result) {
              ParserTask parserTask = new ParserTask();
  
              // Start parsing the Google places in JSON format
              // Invokes the "doInBackground()" method of the class ParserTask
              parserTask.execute(result);
          }
      }
  
      private String downloadUrl(String strUrl) throws IOException
      {
          String data = "";
          InputStream iStream = null;
          HttpURLConnection urlConnection = null;
          try {
              URL url = new URL(strUrl);
  
              // Creating an http connection to communicate with url
              urlConnection = (HttpURLConnection) url.openConnection();
  
              // Connecting to url
              urlConnection.connect();
  
              // Reading data from url
              iStream = urlConnection.getInputStream();
  
              BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
  
              StringBuffer sb = new StringBuffer();
  
              String line = "";
              while ((line = br.readLine()) != null) {
                  sb.append(line);
              }
  
              data = sb.toString();
  
              br.close();
  
          } catch (Exception e) {
              Log.d("Exception", e.toString());
          } finally {
              iStream.close();
              urlConnection.disconnect();
          }
          return data;
      }
  
      private class ParserTask extends AsyncTask&lt;String, Integer, List&lt;HashMap&lt;String, String&gt;&gt;&gt; {
  
          JSONObject jObject;
  
          // Invoked by execute() method of this object
          @Override
          protected List&lt;HashMap&lt;String, String&gt;&gt; doInBackground(String... jsonData) {
  
              List&lt;HashMap&lt;String, String&gt;&gt; places = null;
              Place_JSON placeJson = new Place_JSON();
  
              try {
                  jObject = new JSONObject(jsonData[0]);
  
                  places = placeJson.parse(jObject);
  
              } catch (Exception e) {
                  Log.d("Exception", e.toString());
              }
              return places;
          }
  
          // Executed after the complete execution of doInBackground() method
          @Override
          protected void onPostExecute(List&lt;HashMap&lt;String, String&gt;&gt; list) {
  
              Log.d("Map", "list size: " + list.size());
              // Clears all the existing markers;
              //mGoogleMap.clear();
  
              for (int i = 0; i &lt; list.size(); i++) {
  
                  // Creating a marker
                  MarkerOptions markerOptions = new MarkerOptions();
  
                  // Getting a place from the places list
                  HashMap&lt;String, String&gt; hmPlace = list.get(i);
  
  
                  // Getting latitude of the place
                  double lat = Double.parseDouble(hmPlace.get("lat"));
  
                  // Getting longitude of the place
                  double lng = Double.parseDouble(hmPlace.get("lng"));
  
                  // Getting name
                  String name = hmPlace.get("place_name");
  
                  Log.d("Map", "place: " + name);
  
                  // Getting vicinity
                  String vicinity = hmPlace.get("vicinity");
  
                  latLng = new LatLng(lat, lng);
  
                  // Setting the position for the marker
                  markerOptions.position(latLng);
  
                  markerOptions.title(name + " : " + vicinity);
  
                  markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
  
                  // Placing a marker on the touched position
                  Marker m = mMap.addMarker(markerOptions);
  
                  // ZZZZZZZZZZZZZZZZZZZ
              }
          }
      }
      public class Place_JSON {
  
          /**
           * Receives a JSONObject and returns a list
           */
          public List&lt;HashMap&lt;String, String&gt;&gt; parse(JSONObject jObject) {
  
              JSONArray jPlaces = null;
              try {
                  /** Retrieves all the elements in the 'places' array */
                  jPlaces = jObject.getJSONArray("results");
              } catch (JSONException e) {
                  e.printStackTrace();
              }
              /** Invoking getPlaces with the array of json object
               * where each json object represent a place
               */
              return getPlaces(jPlaces);
          }
  
          private List&lt;HashMap&lt;String, String&gt;&gt; getPlaces(JSONArray jPlaces) {
              int placesCount = jPlaces.length();
              List&lt;HashMap&lt;String, String&gt;&gt; placesList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
              HashMap&lt;String, String&gt; place = null;
  
              /** Taking each place, parses and adds to list object */
              for (int i = 0; i &lt; placesCount; i++) {
                  try {
                      /** Call getPlace with place JSON object to parse the place */
                      place = getPlace((JSONObject) jPlaces.get(i));
                      placesList.add(place);
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }
              }
              return placesList;
          }
  
          /**
           * Parsing the Place JSON object
           */
          private HashMap&lt;String, String&gt; getPlace(JSONObject jPlace)
          {
  
              HashMap&lt;String, String&gt; place = new HashMap&lt;String, String&gt;();
              String placeName = "-NA-";
              String vicinity = "-NA-";
              String latitude = "";
              String longitude = "";
              String reference = "";
  
              try {
                  // Extracting Place name, if available
                  if (!jPlace.isNull("name")) {
                      placeName = jPlace.getString("name");
                  }
  
                  // Extracting Place Vicinity, if available
                  if (!jPlace.isNull("vicinity")) {
                      vicinity = jPlace.getString("vicinity");
                  }
  
                  latitude = jPlace.getJSONObject("geometry").getJSONObject("location").getString("lat");
                  longitude = jPlace.getJSONObject("geometry").getJSONObject("location").getString("lng");
                  reference = jPlace.getString("reference");
  
                  place.put("place_name", placeName);
                  place.put("vicinity", vicinity);
                  place.put("lat", latitude);
                  place.put("lng", longitude);
                  place.put("reference", reference);
  
              } catch (JSONException e) {
                  e.printStackTrace();
              }
              return place;
          }
      }
  }
  </code></pre>