2011-11-08 65 views
1

我正在開發將顯示用戶 當前的位置也顯示醫生在靠近 由他在谷歌map.I開發一個 演示應用程序,它可以找到任何位置 ,把一個標記就可以了,但我不知道一個應用程序如何在 附近找到存儲在數據庫中的地址。如何在Google地圖上存儲在我的數據庫中的醫生地址附近搜索?

**My XML File:** 

    <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
    <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_alignParentBottom="true" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 
      <EditText 
       android:layout_width="fill_parent" 
       android:id="@+id/location" 
       android:layout_height="wrap_content" 
       android:text="White House" /> 
      <Button 
       android:id="@+id/geocodeBtn" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Find Location" /> 
     </LinearLayout> 
     <com.google.android.maps.MapView 
        android:id="@+id/mapview" 
        android:layout_width="fill_parent" 
        android:layout_height="320px" 
        android:clickable="true" 
     android:apiKey=Z1oyeAgSpj5vGQVD6ADrUI2622o7yfJVJ3vEOA" 

        /> 

    </RelativeLayout> 

    **Manifest File:** 

<?xml version="1.0" encoding="utf-8"?> 
<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android" 
     package="pkg.Map" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="7" /> 


    <application 
     android:icon="@drawable/icon" 
     android:label="@string/app_name"> 
    <uses-library 
     android:name="com.google.android.maps"> 
    </uses-library> 
     <activity 
      android:name="pkg.GeoCoderWithMarkers.GeoCoder" 
      android:label="@string/app_name"> 
      <intent-filter> 
       <action 
      android:name="android.intent.action.MAIN" /> 
       <category 
       android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 


    </application> 
    <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"/> 
</manifest> 

和Java文件:

package pkg.GeoCoderWithMarkers; 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 

import pkg.Map.R; 
import android.graphics.Canvas; 
import android.graphics.drawable.Drawable; 
import android.location.Address; 
import android.location.Geocoder; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 

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

public class GeoCoder extends MapActivity { 
    private MapController mapController; 
    private MapView mapView; 
    GeoPoint p,p1; 
    private MyLocationOverlay me=null; 
    private LocationManager locationManager; 
    private Geocoder geocoder = null; 
    int lat; 
    int lng; 

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

     GeoPoint pt = new GeoPoint((int)(5.34079*1000000), 
       (int)(100.28241*1000000)); 
     mapView.getController().setZoom(10); 
     mapView.setStreetView(true); 
     mapView.getController().setCenter(pt); 
     Button geoBtn =(Button)findViewById(R.id.geocodeBtn); 
     geocoder = new Geocoder(this); 
     final Drawable marker=getResources().getDrawable 
      (R.drawable.pushpin_blue); 
     int markerWidth = marker.getIntrinsicWidth(); 
     int markerHeight = marker.getIntrinsicHeight(); 
     marker.setBounds(0, markerHeight, markerWidth, 0); 



     MyItemizedOverlay myItemizedOverlay = 
       new MyItemizedOverlay(marker); 
     mapView.getOverlays().add(myItemizedOverlay); 

     GeoPoint myPoint1 = new GeoPoint(lat, 
       (int)(100.28241*1000000)); 
     myItemizedOverlay.addItem(myPoint1, 
      "myPoint1", "myPoint1"); 
     GeoPoint myPoint2 = new GeoPoint 
      (50*1000000, 50*1000000); 
     myItemizedOverlay.addItem 
      (myPoint2, "myPoint2", "myPoint2"); 


     geoBtn.setOnClickListener(new OnClickListener(){ 
      @Override 
     public void onClick(View arg0) { 
      try { 
      EditText loc = 
       (EditText)findViewById(R.id.location); 

      String locationName = loc.getText().toString(); 
      List<Address> addressList = 
       geocoder.getFromLocationName 
       (locationName, 5); 
      if(addressList!=null && addressList.size()>0) 
      { 
       int lat = (int)(addressList.get(0). 
       getLatitude()*1000000); 
       int lng = (int)(addressList.get(0). 
       getLongitude()*1000000); 
       GeoPoint pt = new GeoPoint(lat,lng); 
       mapView.getController().setZoom(17); 
       mapView.getController().setCenter(pt); 

       MyItemizedOverlay myItemizedOverlay = 
       new MyItemizedOverlay(marker); 
       mapView.getOverlays().add(myItemizedOverlay); 

       GeoPoint myPoint1 = new GeoPoint(lat, lng); 
       myItemizedOverlay.addItem 
       (myPoint1, "myPoint1", "myPoint1"); 
      // GeoPoint myPoint2 = new GeoPoint 
       (50*1000000, 50*1000000); 
      // myItemizedOverlay.addItem 
       (myPoint2, "myPoint2", "myPoint2"); 
      } 
      } catch (IOException e) { 
      e.printStackTrace(); 
      } 
     }}); 



    } 
    @Override 
    protected boolean isRouteDisplayed() { 
    return false; 
    } 


} 
class MyItemizedOverlay extends 
     ItemizedOverlay<OverlayItem>{ 

    private ArrayList<OverlayItem> 
     overlayItemList = new ArrayList<OverlayItem>(); 

    public MyItemizedOverlay(Drawable marker) { 
    super(boundCenterBottom(marker)); 
    // TODO Auto-generated constructor stub 

    populate(); 
    } 



    public void addItem(GeoPoint p, 
      String title, String snippet){ 
    OverlayItem newItem = 
     new OverlayItem(p, title, snippet); 
    overlayItemList.add(newItem); 
     populate(); 
    } 

    @Override 
    protected OverlayItem createItem(int i) { 
    // TODO Auto-generated method stub 
    return overlayItemList.get(i); 
    } 

    @Override 
    public int size() { 
    // TODO Auto-generated method stub 
    return overlayItemList.size(); 
    } 

    @Override 
    public void draw(Canvas canvas, 
      MapView mapView, boolean shadow) { 
    // TODO Auto-generated method stub 
    super.draw(canvas, mapView, shadow); 
    //boundCenterBottom(marker); 
    } 

} 
+0

你的數據庫是否包含緯度和經度等字段?如果是這樣,您可以獲取當前的經度和緯度,並根據此位置搜索最低距離的經度和緯度。你可以找到現成的,兩個緯度之間的距離計算。基於距離你可以搜索附近醫生 – Lucifer

回答

1

Location -Class爲您提供一個很不錯的方法:distanceTo(GeoPoint)。 因此,只需使用此方法遍歷醫生的GeoPoint即可。

0

您可以定義具體的半徑並計算從您的位置到醫生地址的距離,並找到距離並按照半徑集合檢查它是否在半徑內,然後通過醫生地址顯示附近。

爲此,您需要檢查每次當該位置更改爲服務,以便您可以找到附近的位置。

+0

你能給我一個小例子嗎? –

相關問題