den_gera
den_gera

Reputation: 77

marker in my location?

Please help me, how can I set a marker in current location? Below my code onCreate, addMarker and createMapView.

onCreate public class MapActivity extends Activity {

GoogleMap googleMap;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_activity);

    createMapView();
    addMarker();

}

addMarker

private void addMarker() {
    if (null != googleMap) {
        googleMap.addMarker(new MarkerOptions()
                .title("Marker")
                .position(new LatLng(0, 0))
                .draggable(true));
    }
}

createMapView

private void createMapView() {
    try {
        if (null == googleMap) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapView)).getMap();


            if (null == googleMap) {
                Toast.makeText(getApplicationContext(), "Error creating map", Toast.LENGTH_LONG).show();
            }
        }
    } catch (NullPointerException e) {
        Log.e("mapApp", e.toString());
    }
}

Upvotes: 0

Views: 82

Answers (4)

Vishal Patoliya ツ
Vishal Patoliya ツ

Reputation: 3238

You must get your lat and lng of your current location and put this code.

 @Override
    public void onMapReady(GoogleMap map) {
        map.addMarker(new MarkerOptions()
            .position(new LatLng(10, 10))
            .title("Hello world"));
    }

https://developers.google.com/maps/documentation/android-api/marker

Upvotes: 0

AdrianH
AdrianH

Reputation: 86

You are setting your marker at (0, 0) .position(new LatLng(0, 0))

You can get the latest position in onLocationChanged.

Get the latitude and longitude: new LatLng(location.getLatitude(), location.getLongitude())

Use the LatLng object to set your marker.

Upvotes: 2

Pratik
Pratik

Reputation: 456

First of all you need to get current latlng of your position and create latlng object based on it , and when you get latlng successfully use this method

        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
               Criteria criteria = new Criteria();
               String bestProvider = locationManager.getBestProvider(criteria, true);
                fromPosition = new LatLng(latti, longi);



googleMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.orangedoticon)).title("helo0o0"));

Upvotes: 0

Mustansar Saeed
Mustansar Saeed

Reputation: 2790

Get the latitude and longitude location from FusedLocationApi as GoogleApiClient.FusedLocationApi.getLastLocation() and then call addMarker using these lat/longs.

Hope this helps.

Upvotes: 0

Related Questions