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 6324241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:40:03+00:00 2026-05-24T16:40:03+00:00

I am writing a program that gets the coordinates of where a cellphone is

  • 0

I am writing a program that gets the coordinates of where a cellphone is using the telephony manager, the problem is, it gives me the coordinates of the cell tower to which is closest to me but not the exact location where I am. Here is my code. How do use the coordiantes of the cell tower to give me my exct location. Thanx guys

package com.celllocation;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.widget.TextView;
import android.widget.Toast;

public class cellLocation extends Activity {
    int cellID,lac;
    TextView textViewCoord;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textViewCoord=(TextView)findViewById(R.id.textViewCoord);
    }
    @Override
    protected void onStart() {
        TelephonyManager telManager=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        GsmCellLocation location=(GsmCellLocation)telManager.getCellLocation();
        cellID=location.getCid();
        lac=location.getLac();
        try
        {
            displayMap(cellID, lac);
        }
        catch(Exception e){
            e.printStackTrace();
        }

        super.onStart();
    }
    private boolean displayMap(int cellID, int lac) throws Exception 
    {
        String urlString = "http://www.google.com/glm/mmap";            

        //---open a connection to Google Maps API---
        URL url = new URL(urlString); 
        URLConnection conn = url.openConnection();
        HttpURLConnection httpConn = (HttpURLConnection) conn;        
        httpConn.setRequestMethod("POST");
        httpConn.setDoOutput(true); 
        httpConn.setDoInput(true);
        httpConn.connect(); 

        //---write some custom data to Google Maps API---
        OutputStream outputStream = httpConn.getOutputStream();
        WriteData(outputStream, cellID, lac);       

        //---get the response---
        InputStream inputStream = httpConn.getInputStream();  
        DataInputStream dataInputStream = new DataInputStream(inputStream);

        //---interpret the response obtained---
        dataInputStream.readShort();
        dataInputStream.readByte();
        int code = dataInputStream.readInt();
        if (code == 0) {
            double lat = (double) dataInputStream.readInt() / 1000000D;
            double lng = (double) dataInputStream.readInt() / 1000000D;
            dataInputStream.readInt();
            dataInputStream.readInt();
            dataInputStream.readUTF();

            //---display Google Maps---
            String uriString =lat+ "," + lng;
            Toast.makeText(getApplicationContext(), uriString, Toast.LENGTH_LONG).show();
            textViewCoord.setText(uriString);
            return true;
        }
        else
        {           
            return false;
        }
    } 
    private void WriteData(OutputStream out, int cellID, int lac) throws IOException
    {       
        DataOutputStream dataOutputStream = new DataOutputStream(out);
        dataOutputStream.writeShort(21);
        dataOutputStream.writeLong(0);
        dataOutputStream.writeUTF("en");
        dataOutputStream.writeUTF("Android");
        dataOutputStream.writeUTF("1.0");
        dataOutputStream.writeUTF("Web");
        dataOutputStream.writeByte(27);
        dataOutputStream.writeInt(0);
        dataOutputStream.writeInt(0);
        dataOutputStream.writeInt(3);
        dataOutputStream.writeUTF("");

        dataOutputStream.writeInt(cellID);  
        dataOutputStream.writeInt(lac);     

        dataOutputStream.writeInt(0);
        dataOutputStream.writeInt(0);
        dataOutputStream.writeInt(0);
        dataOutputStream.writeInt(0);
        dataOutputStream.flush();       
    }
}
  • 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-05-24T16:40:04+00:00Added an answer on May 24, 2026 at 4:40 pm

    For the exact location you need to use the GPS, for a less accurate location you can use NETWORK_PROVIDER (varying accuracy, depending on density of cellular nework).
    I think that you might need to define a LocationManager in the onCreate

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    

    or for network provider:

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    

    This class provides access to the system location services. These services allow applications to obtain periodic updates of the device’s geographical location, or to fire an application-specified Intent when the device enters the proximity of a given geographical location.

    Marco

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

Sidebar

Related Questions

I'm writing a program that gets all directories and sub-directories. I'm using the following
I am writing a small program that gets activated when sms is received.We need
I am using libcurl for a small program that gets data from an input
I'm writing a program that, using Rijndael, will encrypt and decrypt files/folders using a
I am writing a program that gets a HTML file in Android and I
I'm writing a program that randomly assembles mathematical expressions using the values stored in
Im writing a program which uses simple neural network perception algorithm, I have problem
I'm writing a python3 program, that gets the names of files to process from
Im writing a program that should read input via stdin, so I have the
I'm writing a program that sends an email out at a client's specific local

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.