Note that there are some explanatory texts on larger screens.

plurals
  1. POGoogle Maps v2 giving wrong location
    primarykey
    data
    text
    <p>Whenever I try and use the set Marker button, a marker is set up but just in the wrong location. When I hit the button provided by googleMap.setMyLocationEnabled(true) the map moves to the blue dot in the exact position. How come it gets the current location and i'm getting a random location? </p> <pre><code>package com.example.googlemaps; //import java.io.IOException; //import java.util.List; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; //import android.location.Address; import android.location.Criteria; //import android.location.Geocoder; import android.location.Location; import android.location.LocationManager; import android.os.Bundle; //import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.support.v4.app.FragmentActivity; public class MainActivity extends FragmentActivity { private GoogleMap googleMap; double currentLat, currentLong; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setUpMapIfNeeded(); Button setLocMarker = (Button)findViewById(R.id.set_marker); setLocMarker.setOnClickListener(setMarkerLocation); } private OnClickListener setMarkerLocation = new OnClickListener(){ public void onClick(View v) { setMarker(currentLat, currentLong); } }; private void setUpMapIfNeeded(){ // Do a null check to confirm that we have not already instantiated the map. if(googleMap == null){ //Try to obtain the map from the SupportMapFragment googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); //check if we were successful in obtaining the map if(googleMap != null){ setUpMap(); } } } private void setUpMap() { getCurrentLocation(); //set map type googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID ); //creates a LatLng object for current location LatLng latLng = new LatLng(currentLat, currentLong); //show the current location in Google Map googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); //Zoom in the Google Map googleMap.animateCamera(CameraUpdateFactory.zoomTo(20)); //setMarker(latitude, longitude); } public void getCurrentLocation() { //Enable MyLocation Layer of Google Map googleMap.setMyLocationEnabled(true); //Get LocationManager object from System Service LOCATION_SERVICE LocationManager locationManager = (LocationManager) getSystemService(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); //Get latitude of current location currentLat = myLocation.getLatitude(); //Get longitude of current location currentLong = myLocation.getLongitude(); } public void setMarker(double latitude, double longitude){ googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("Your set Marker!")); } // public void getLocFromAddress(String address){ // // final Geocoder geoCoder = new Geocoder(); // // // try{ // List&lt;Address&gt; addresses = geoCoder.getFromLocationName(address, 1); // if(addresses.size() &gt; 0){ // double latitude = addresses.get(0).getLatitude(); // double longitude = addresses.get(0).getLongitude(); // } // }catch(IOException e){ // // } // // // // } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload