Simple Google Maps In Android
I am trying to execute MAP-API program in emulator I am getting the log errors ! r there any logic errors How to resolve these errors MainActivity.java public class MainActivity
Solution 1:
try to extend Activity rather then fragmentActivity
.java file:
publicclassMainActivityextendsActivityimplementsLocationListener{
LocationManager locationManager;
private GoogleMap googleMap;
double lat1=17.385044;
doublelong=78.486671;
private String provider;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager service=(LocationManager)getSystemService(LOCATION_SERVICE);
boolean enableGPS= service.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean enableWifi=service.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
// Check if enabled and if not send user to the GSP settings// Better solution would be to display a dialog and suggesting to // go to the settingsif(!enableGPS){
Toast.makeText(getApplicationContext(), "Gps signal not found",1).show();
Intent intent=newIntent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteriacriteria=newCriteria();
provider = locationManager.getBestProvider(criteria, false);
Locationlocation= locationManager.getLastKnownLocation(provider);
// Initialize the location fieldsif (location != null) {
Toast.makeText(this, "Selected Provider " + provider, 0).show();
onLocationChanged(location);
} else {
//do something
}
try {
//loading map
initilizeMap();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
privatevoidinitilizeMap(){
if(googleMap==null){
googleMap=(MapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
//check if map is created succesfully or notif(googleMap==null){
Toast.makeText(getApplicationContext(),"sorry unable to create map",1).show();
}
}
MarkerOptions markerOptions=new MarkerOptions().position(new LatLng(lat1, long)); markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)); CameraPosition cameraPosition=new CameraPosition.Builder().target(new LatLng(17.385044, 78.486671)).zoom(12).build(); googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); googleMap.setMyLocationEnabled(true); googleMap.setOnInfoWindowClickListener(null); }
@OverrideprotectedvoidonResume(){
super.onResume();
initilizeMap();
}
/* Remove the locationlistener updates when Activity is paused */@OverrideprotectedvoidonPause() {
super.onPause();
locationManager.removeUpdates(this);
}
publicvoidonLocationChanged(Location location) {
double lat = location.getLatitude();
double lng = location.getLongitude();
Toast.makeText(this, "Location " + lat+","+lng,
Toast.LENGTH_LONG).show();
LatLng coordinate = newLatLng(lat, lng);
Toast.makeText(this, "Location " + coordinate.latitude+","+coordinate.longitude,
Toast.LENGTH_LONG).show();
Marker startPerc = googleMap.addMarker(newMarkerOptions()
.position(coordinate)
.title("Start")
.snippet("Inizio del percorso")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
}
publicvoidonProviderDisabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,0).show();
}
publicvoidonProviderEnabled(String provider) {
Toast.makeText(this, "Disabled provider " + provider,0).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub
}
Post a Comment for "Simple Google Maps In Android"