Reputation: 31
I have this codes but when im trying to display the marker its not showing but i can view my current location. but at first these codes are working properly.
package com.fatima.lokalngrizal;
import com.google.android.gms.location.LocationListener;
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.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class RizalMap extends FragmentActivity implements LocationListener {
GoogleMap googleMap;
LatLng myPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rizal_map);
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
//GoogleMap object from fragment
googleMap = fm.getMap();
// Enabling MyLocation Layer of google map
googleMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
//retrieve provider..
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
// kuha current location
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
// kuha latitude ng location mo
double latitude = location.getLatitude();
// kuha longitude ng location mo
double longitude = location.getLongitude();
// creating latlng ng location mo ngayun
LatLng latLng = new LatLng(latitude, longitude);
myPosition = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions().position(myPosition).title("You are HERE !"));
}
}
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
}
}
I even try this codes still the maker is not showing. i just want to see some markers to my map
map.addMarker(new MarkerOptions()
.position(new LatLng(14.7748641,121.2433147))
.title("Montalban Town Center")
Upvotes: 3
Views: 4967
Reputation: 1206
I was getting same problem.
When I was getting the latlang value was
LatLng location = new LatLng(latitude, longitude);
Then I made my latitude value negative and it worked,
LatLng location = new LatLng(-1*latitude, longitude);
Upvotes: 0
Reputation: 6921
This probably means your location variable is still null, so your codes inside the if statement are not been called. Your location manager probably need to call the requestLocationUpdates
method.
For for information about Location Manager, you can check out this tutorial.
Or this tutorial about Google Maps Markers.
For now, you can also try it without location manager, below are the sample code:
Marker hamburg = googleMap.addMarker(new MarkerOptions().position(new LatLng(53.558, 9.927))
.title("Hamburg"));
Marker kiel = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(53.551, 9.993))
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to hamburg with a zoom of 15.
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(53.558, 9.927), 15));
// Zoom in, animating the camera.
googleMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
Upvotes: 2