在android 2.2中实现地理编码

发布于 2024-12-07 13:55:06 字数 74 浏览 0 评论 0原文

朋友们... 我正在开发一个关于地理编码的项目。我尝试多次实现它。但是我无法检索与某个位置相对应的纬度和经度值。请帮助我完成我的项目。

Friends...
i'm woking on a project on geocoding.I tried to implement it several times.But i'm not able to retrieve the latitude and longitude values corresponding to a location.Please help me out to complete my project..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失退 2024-12-14 13:55:07

尝试这段代码希望这会对您有所帮助:

package com.example.map;  
import java.util.List;
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 android.app.AlertDialog;  
import android.app.Dialog;  
import android.location.Address;  
import android.location.Geocoder;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.EditText; 

public class mapView extends MapActivity{

MapView myMap;
Button btnSearch;
EditText adress;
Geocoder gc;
double lat;
double lon;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myMap = (MapView) findViewById(R.id.simpleGM_map); // Get map from XML
    btnSearch = (Button) findViewById(R.id.simpleGM_btn_search); // Get button from xml
    adress = (EditText) findViewById(R.id.simpleGM_adress); // Get address from XML

    gc = new Geocoder(this); // create new geocoder instance
    btnSearch.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v)
        {
            String addressInput = adress.getText().toString(); // Get input text

            try 
            {

                List<Address> foundAdresses = gc.getFromLocationName(
                        addressInput, 5); // Search addresses

                if (foundAdresses.size() == 0) 
                { // if no address found,
                    // display an error
                    Dialog locationError = new AlertDialog.Builder(
                            mapView.this).setIcon(0).setTitle(
                            "Error").setPositiveButton(R.string.ok, null)
                            .setMessage(
                                    "Sorry, your address doesn't exist.")
                            .create();
                    locationError.show();
                } 
                else 
                { // else display address on map
                    for (int i = 0; i < foundAdresses.size(); ++i) 
                    {
                        // Save results as Longitude and Latitude
                        // @todo: if more than one result, then show a
                        // select-list
                        Address x = foundAdresses.get(i);
                        lat = x.getLatitude();
                        lon = x.getLongitude();
                    }
                    navigateToLocation((lat * 1000000), (lon * 1000000),
                            myMap); // display the found address
                }
            } 
            catch (Exception e) 
            {
                // @todo: Show error message
            }

        }
    });
}


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


/
 * Navigates a given MapView to the specified Longitude and Latitude          
 * @param latitude
 * @param longitude
 * @param mv
 */
public static void navigateToLocation(double latitude, double longitude,
        MapView mv) 
{
    GeoPoint p = new GeoPoint((int) latitude, (int) longitude); // new
    // GeoPoint
    mv.displayZoomControls(true); // display Zoom (seems that it doesn't
    // work yet)
    MapController mc = mv.getController();
    mc.animateTo(p); // move map to the given point
    int zoomlevel = mv.getMaxZoomLevel(); // detect maximum zoom level
    mc.setZoom(zoomlevel - 1); // zoom
    mv.setSatellite(false); // display only "normal" mapview

}
}

try this code hope this will help you:

package com.example.map;  
import java.util.List;
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 android.app.AlertDialog;  
import android.app.Dialog;  
import android.location.Address;  
import android.location.Geocoder;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.EditText; 

public class mapView extends MapActivity{

MapView myMap;
Button btnSearch;
EditText adress;
Geocoder gc;
double lat;
double lon;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myMap = (MapView) findViewById(R.id.simpleGM_map); // Get map from XML
    btnSearch = (Button) findViewById(R.id.simpleGM_btn_search); // Get button from xml
    adress = (EditText) findViewById(R.id.simpleGM_adress); // Get address from XML

    gc = new Geocoder(this); // create new geocoder instance
    btnSearch.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v)
        {
            String addressInput = adress.getText().toString(); // Get input text

            try 
            {

                List<Address> foundAdresses = gc.getFromLocationName(
                        addressInput, 5); // Search addresses

                if (foundAdresses.size() == 0) 
                { // if no address found,
                    // display an error
                    Dialog locationError = new AlertDialog.Builder(
                            mapView.this).setIcon(0).setTitle(
                            "Error").setPositiveButton(R.string.ok, null)
                            .setMessage(
                                    "Sorry, your address doesn't exist.")
                            .create();
                    locationError.show();
                } 
                else 
                { // else display address on map
                    for (int i = 0; i < foundAdresses.size(); ++i) 
                    {
                        // Save results as Longitude and Latitude
                        // @todo: if more than one result, then show a
                        // select-list
                        Address x = foundAdresses.get(i);
                        lat = x.getLatitude();
                        lon = x.getLongitude();
                    }
                    navigateToLocation((lat * 1000000), (lon * 1000000),
                            myMap); // display the found address
                }
            } 
            catch (Exception e) 
            {
                // @todo: Show error message
            }

        }
    });
}


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


/
 * Navigates a given MapView to the specified Longitude and Latitude          
 * @param latitude
 * @param longitude
 * @param mv
 */
public static void navigateToLocation(double latitude, double longitude,
        MapView mv) 
{
    GeoPoint p = new GeoPoint((int) latitude, (int) longitude); // new
    // GeoPoint
    mv.displayZoomControls(true); // display Zoom (seems that it doesn't
    // work yet)
    MapController mc = mv.getController();
    mc.animateTo(p); // move map to the given point
    int zoomlevel = mv.getMaxZoomLevel(); // detect maximum zoom level
    mc.setZoom(zoomlevel - 1); // zoom
    mv.setSatellite(false); // display only "normal" mapview

}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文