title: cannot display current position on google map v2
body: <p>I am developing an android app where a map is displayed with markers dispayed on it. I try to read and display each time my current location using WI-FI/3G/GPS on my mobile android device. The problem i have is that I cannot see the tittle printed and the snippet on my map.
  my code is the following. </p>
  
  <p>private void setUpMap() {</p>
  
  <pre><code>    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
  
  
      // Enable MyLocation Layer of Google Map
      mMap.setMyLocationEnabled(true);
  
      // Enable / Disable my location button
      mMap.getUiSettings().setMyLocationButtonEnabled(true);
  
      // Get LocationManager object from System Service LOCATION_SERVICE
      LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  
      // Create a criteria object to retrieve provider
      Criteria criteria = new Criteria();
  
      // Get the name of the best provider
      String provider = locationManager.getBestProvider(criteria, true);
  
  
      // Get Current Location
      Location myLocation = locationManager.getLastKnownLocation(provider);
  
  
      // set map type
      mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);   // MAP_TYPE_HYBRID
  
           // Get latitude of the current location
           double latitude = myLocation.getLatitude();
  
           // Get longitude of the current location
           double longitude = myLocation.getLongitude();
  
           // Create a LatLng object for the current location
           LatLng latLng = new LatLng(latitude, longitude);
  
           // Show the current location in Google Map
           mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
  
           // Zoom in the Google Map
           mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
           mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("I am here!").snippet("success located"));
  
       }
  
  }
  </code></pre>
  
  <p>Based on the above code of my Maps Activity.java file, i never see the tittle " I am here" because of error null pointer exception for:</p>
  
  <p>double latitude = location.getLatitude();  </p>
  
  <p>How can I resolve this issue?</p>
  
  <p>I tried the following yesterday for testing purposes:</p>
  
  <p>// Enable MyLocation Layer of Google Map</p>
  
  <pre><code>    mMap.setMyLocationEnabled(true);
  
      // Enable / Disable my location button
      mMap.getUiSettings().setMyLocationButtonEnabled(true);
  
      // Get LocationManager object from System Service LOCATION_SERVICE
      LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  
      // Create a criteria object to retrieve provider
      Criteria criteria = new Criteria();
  
      // Get the name of the best provider
      String provider = locationManager.getBestProvider(criteria, true);
  
  
      // Get Current Location
      Location myLocation = locationManager.getLastKnownLocation(provider);
  
  
      // set map type
      mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);   // MAP_TYPE_HYBRID
  
       if(myLocation != null) {
           // Get latitude of the current location
           double latitude = myLocation.getLatitude();
  
           // Get longitude of the current location
           double longitude = myLocation.getLongitude();
  
           // Create a LatLng object for the current location
           LatLng latLng = new LatLng(latitude, longitude);
  
           // Show the current location in Google Map
           mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
  
           // Zoom in the Google Map
           mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
           mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("I am here!").snippet("Consider yourself located"));
                          } //case myLocation not null
                               else { //do nothing
                                                   }
       }
  
  }
  </code></pre>
  
  <p>In tried to open my api at least 10 times after connecting to 3 different places with wi-fi connection. 90% of the times I opened my api I saw my correct current position in a blue color circle. 10% of the times i opened my api I saw a red marker displaying the message "I am here!"(usually when I disable and then enable position settings on my mobile) together with the blue color circle...This means that most of the times I tried to get location the provider (network: wi-fi) failed? Is this a provider problem...? </p>
  
  <p>In the past I developed another project web interface based with javascript, CSS, html code. I used getCurrentLocation-javascript which 100% displayed the correct current location (10 different places I tried in a single day and all my current Locations I got where correct). Is javascript get current position functionality more reliable than java?</p>