Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8213583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:10:10+00:00 2026-06-07T11:10:10+00:00

My program should be able to: Change the map view Zoom in to the

  • 0

My program should be able to:

  1. Change the map view
  2. Zoom in to the phone’s current location
  3. Add a marker
  4. Display an alert of the Latitude and Longitude values

When I use GPS_PROVIDER, functions 1-4 works in my emulator (because I manually feed it a latitude and longitude values). But when I created an apk file, 2-4 functions don’t work.

Here’s my class.

package com.locator.map;

import java.text.DecimalFormat;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class Main extends MapActivity
{
    MapController controller;
    double currentLat;
    double currentLon;
    LocationManager manager;
    boolean locationChanged = false;
    Button retrieveLocationButton, toggleViewButton;
    boolean streetview = false;
    Drawable d;
    List<Overlay> overlaylist;
    MapView map;
    Criteria criteria;
    String bestProvider;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);      
        map = (MapView) findViewById (R.id.googleMap);       
        map.setBuiltInZoomControls(true);
        map.setSatellite(true);
        controller = map.getController();


        criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        manager = (LocationManager) this.getSystemService (Context.LOCATION_SERVICE);        
        bestProvider = manager.getBestProvider(criteria, true);

        retrieveLocationButton = (Button) findViewById(R.id.retrieveLocation);
        toggleViewButton = (Button) findViewById(R.id.changeView);

        retrieveLocationButton.setOnClickListener(new OnClickListener()
        {      
                public void onClick(View v)
                {
                    showCurrentLocation();
                }
        });        

        toggleViewButton.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                if (streetview == false)
                {
                    map.setSatellite(false);
                    map.setStreetView(true);
                    streetview=true;
                }
                else if (streetview == true)
                {
                    map.setStreetView(false);
                    map.setSatellite(true);
                    streetview=false;
                }
            }           
        });
        LocationListener listener = new LocationListener()
        {
            public void onLocationChanged(Location location)
            {
                // TODO Auto-generated method stub

                //Retrieve the current GPS coordinates
                currentLat = location.getLatitude();
                currentLon = location.getLongitude();

                locationChanged = true;
            }

            public void onProviderDisabled(String provider)
            {
                // TODO Auto-generated method stub
                Toast.makeText(getBaseContext(), "Your GPS services are not activated.", Toast.LENGTH_LONG).show();
            }

            public void onProviderEnabled(String provider)
            {
                // TODO Auto-generated method stub
                Toast.makeText(getBaseContext(), "Your GPS services are now activated.", Toast.LENGTH_LONG).show();
            }

            public void onStatusChanged(String provider, int status, Bundle extras)
            {
                // TODO Auto-generated method stub
                Toast.makeText(getBaseContext(), "Your GPS service provider is changed.", Toast.LENGTH_LONG).show();
            }

        };

        manager.requestLocationUpdates(bestProvider, 1000, 1, listener); //--> To update current location (coordinates [latitude & longitude]).       

    }

    protected void showCurrentLocation()
    {
        // TODO Auto-generated method stub

        //If location is changed or updated - Zoom in to current location, add a marker and show a dialog with current coordinates.
        if (locationChanged == true)
        {
            //Set the map's view and zoom level animation
            GeoPoint currentLocation = new GeoPoint ((int) (currentLat * 1E6), (int) (currentLon * 1E6));

            controller.setCenter(currentLocation);
            controller.setZoom(15);

            //Show the current coordinates (Convert to String and round off - 6 decimal places)
            String printLat = new DecimalFormat("0.######").format((double)currentLat);
            String printLon = new DecimalFormat("0.######").format((double)currentLon);

            AlertDialog alert = new AlertDialog.Builder(Main.this).create();
            alert.setTitle("Current Location:");
            alert.setMessage("Latitude: " + printLat + "\n" + "Longitude: " + printLon);
            alert.setButton("Close", new DialogInterface.OnClickListener()
            {               
                public void onClick(DialogInterface arg0, int arg1)
                {
                    // TODO Auto-generated method stub
                    //Alert Dialog won't work without a listener
                    //Do nothing (This will simply close your alert dialog)
                }
            });     
            alert.show();

            //Add a marker
            overlaylist = map.getOverlays();
            d = getResources().getDrawable(R.drawable.ic_launcher);         
            CustomPinpoint marker = new CustomPinpoint (d, Main.this);          
            OverlayItem overlayitem = new OverlayItem (currentLocation, "1st String", "2nd String");
            marker.insertPinpoint(overlayitem);
            overlaylist.add(marker);    
        }
    }

    @Override
    protected boolean isRouteDisplayed()
    {
        // TODO Auto-generated method stub
        return false;
    }
}

I also tried using NETWORK_PROVIDER.
Everything works in my phone but it’s not returning accurate latitude and longitude values. It adds marker but it’s about 1 kilometer away from my actual location.

Here’s my class using NETWORK_PROVIDER:

package com.locator.map;

import java.text.DecimalFormat;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class Main extends MapActivity
{
    MapController controller;
    double currentLat;
    double currentLon;
    LocationManager manager;
    boolean locationChanged = false;
    Button retrieveLocationButton, toggleViewButton;
    boolean streetview = false;
    Drawable d;
    List<Overlay> overlaylist;
    MapView map;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);      
        map = (MapView) findViewById (R.id.googleMap);       
        map.setBuiltInZoomControls(true);
        map.setSatellite(true);
        controller = map.getController();


        manager = (LocationManager) this.getSystemService (Context.LOCATION_SERVICE);        

        retrieveLocationButton = (Button) findViewById(R.id.retrieveLocation);
        toggleViewButton = (Button) findViewById(R.id.changeView);

        retrieveLocationButton.setOnClickListener(new OnClickListener()
        {      
                public void onClick(View v)
                {
                    showCurrentLocation();
                }
        });        

        toggleViewButton.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                if (streetview == false)
                {
                    map.setSatellite(false);
                    map.setStreetView(true);
                    streetview=true;
                }
                else if (streetview == true)
                {
                    map.setStreetView(false);
                    map.setSatellite(true);
                    streetview=false;
                }
            }           
        });
        LocationListener listener = new LocationListener()
        {
            public void onLocationChanged(Location location)
            {
                // TODO Auto-generated method stub

                //Retrieve the current GPS coordinates
                currentLat = location.getLatitude();
                currentLon = location.getLongitude();

                locationChanged = true;
            }

            public void onProviderDisabled(String provider)
            {
                // TODO Auto-generated method stub
                Toast.makeText(getBaseContext(), "Your GPS services are not activated.", Toast.LENGTH_LONG).show();
            }

            public void onProviderEnabled(String provider)
            {
                // TODO Auto-generated method stub
                Toast.makeText(getBaseContext(), "Your GPS services are now activated.", Toast.LENGTH_LONG).show();
            }

            public void onStatusChanged(String provider, int status, Bundle extras)
            {
                // TODO Auto-generated method stub
                Toast.makeText(getBaseContext(), "Your GPS service provider is changed.", Toast.LENGTH_LONG).show();
            }

        };

        manager.requestLocationUpdates(NETWORK_PROVIDER, 1000, 1, listener); //--> To update current location (coordinates [latitude & longitude]).       

    }

    protected void showCurrentLocation()
    {
        // TODO Auto-generated method stub

        //If location is changed or updated - Zoom in to current location, add a marker and show a dialog with current coordinates.
        if (locationChanged == true)
        {
            //Set the map's view and zoom level animation
            GeoPoint currentLocation = new GeoPoint ((int) (currentLat * 1E6), (int) (currentLon * 1E6));

            controller.setCenter(currentLocation);
            controller.setZoom(15);

            //Show the current coordinates (Convert to String and round off - 6 decimal places)
            String printLat = new DecimalFormat("0.######").format((double)currentLat);
            String printLon = new DecimalFormat("0.######").format((double)currentLon);

            AlertDialog alert = new AlertDialog.Builder(Main.this).create();
            alert.setTitle("Current Location:");
            alert.setMessage("Latitude: " + printLat + "\n" + "Longitude: " + printLon);
            alert.setButton("Close", new DialogInterface.OnClickListener()
            {               
                public void onClick(DialogInterface arg0, int arg1)
                {
                    // TODO Auto-generated method stub
                    //Alert Dialog won't work without a listener
                    //Do nothing (This will simply close your alert dialog)
                }
            });     
            alert.show();

            //Add a marker
            overlaylist = map.getOverlays();
            d = getResources().getDrawable(R.drawable.ic_launcher);         
            CustomPinpoint marker = new CustomPinpoint (d, Main.this);          
            OverlayItem overlayitem = new OverlayItem (currentLocation, "1st String", "2nd String");
            marker.insertPinpoint(overlayitem);
            overlaylist.add(marker);    
        }
    }

    @Override
    protected boolean isRouteDisplayed()
    {
        // TODO Auto-generated method stub
        return false;
    }
}

Here’s my Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.locator.map"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8"  android:targetSdkVersion="8"/>
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <application android:icon="@drawable/ic_launcher" android:label="Locator" >
        <uses-library android:name="com.google.android.maps" />     
        <activity
            android:name=".Main"
            android:label="Locator" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>        
    </application>

</manifest>

I think, GPS is working here in my area, Google Maps application returns accurate location when I use it.
I tried using my application (both NETWORK_PROVIDER and GPS_PROVIDER) in the same location where I used Google Maps:

  1. NETWORK_PROVIDER still returns inaccurate result.
  2. GPS_PROVIDER still doesn’t work.

Please help me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T11:10:12+00:00Added an answer on June 7, 2026 at 11:10 am

    Are you sure Google Maps is using GPS as it’s provider? I find that GMaps often shows locations (and pretty precise ones at times!) using only data from Wi-Fi and GSM antennaes… In that case, your code may never be called (until there’s a GPS fix).

    I would try adding

    locationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, 
                    MINIMUM_TIME_BETWEEN_UPDATES, 
                    MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
                    new MyLocationListener());
    

    to your code (below the registration to get GPS updates), just to check that possibility.

    Edit

    Try changing ACCURACY_FINE to ACCURACY_HIGH

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a bug in this program where it should be able
I am creating a program which should be able to open different perspectives .
my program should print a message on the screen if the formula that the
This program should calculate the value of 2^1+2^2 + ... + 2^10: #include <stdio.h>
What the program should do: After the user clicks the delete button with a
The following line in my C program should provided All/Group/Owner read and write permissions
The assignment at my first year uni computing course says my program should read
If a user types in [[0,0,0], [0,0,1], [1,1,0]] and press enter, the program should
I have this program that should execute a piece of code base on the
Im writing a program that should read input via stdin, so I have the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.