2012-04-22 41 views
6

Mã của tôi được yêu cầu tìm ra vị trí của người dùng và đặt điểm đánh dấu trên bản đồ khi nhập ứng dụng. Giá trị vị trí của tôi luôn bằng null và không bao giờ nhận được giá trị.Google maps api không đặt điểm đánh dấu ở vị trí tốt

if (location != null) { 
       lat = (int) (location.getLatitude() * 1E6); 
       longi = (int) (location.getLongitude() * 1E6); 
       GeoPoint ourLocation = new GeoPoint(lat, longi); 
       OverlayItem overlayItem = new OverlayItem(ourLocation, "AYO", 
         "Whats good yo"); 
       CustomPinpoint custom = new CustomPinpoint(d, CampusMap.this); 
       custom.insertPinpoint(overlayItem); 
       overlayList.add(custom); 

      } else { 
       Toast.makeText(CampusMap.this, "Couldn't get provider", 
         Toast.LENGTH_SHORT).show(); 

      } 

     } 
+0

Có thể tìm kiếm trên google của bạn chưa sẵn sàng tại thời điểm bạn nhập câu lệnh if? Tôi sử dụng api bản đồ google javascript và có chức năng gọi lại được gọi khi tìm kiếm hoàn tất. Không biết nó hoạt động như thế nào trên Android. –

+0

Tham khảo [LINK] này (http://stackoverflow.com/questions/9990209/cant-mark-while-showing-the-current-location-in-mapview/9990668#9990668) nó có giải thích rõ ràng và rõ ràng về yêu cầu và cách đặt điểm đánh dấu. –

+0

Tôi có thể đặt điểm đánh dấu tốt, vấn đề của tôi là nó không tốt vị trí khi bắt đầu. Tôi cũng đã đặt mã này vào phương thức tạo của tôi, nó chỉ giữ hy vọng thẳng vào câu lệnh khác của tôi. nó có liên quan gì đến quyền của tôi không? –

Trả lời

1

Tôi đã có một vấn đề tương đối giống với một game nhập vai GPS tôi đã làm việc trên và đây là một số những điều tôi nhận thấy:

Thứ nhất, nó có thể mất một thời gian cho vị trí của bạn ban đầu được tìm thấy, trong đó sẽ gây ra vấn đề đó vì bạn chỉ kiểm tra xem vị trí có rỗng không.

Bạn cũng có thể muốn chắc chắn dịch vụ định vị của thiết bị đang thực sự được kích hoạt trước khi làm bất cứ điều gì:

private boolean doLocationsCheck(){ 
    if(!checkLocationEnabled()){ 

     final CharSequence[] items = {"Yes", "No"}; 

     AlertDialog.Builder builder = new AlertDialog.Builder(mContext); 
     builder.setCancelable(false); 
     builder.setTitle("Location must be enabled to play this game! Would you like to enable it now?"); 
     builder.setItems(items, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int item) { 
       final int i = item; 
       runOnUiThread(new Runnable() { 
        public void run() {       
         if(i == 0){ 
          Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
          startActivity(intent); 
          quit(); 
         } 
         else{ 
          quit(); 
         } 

        } 
       }); 
      } 


     }).show(); 
     AlertDialog alert = builder.create(); 

     return false; 

    } 
    else { 
     return true; 
    } 
} 

private boolean checkLocationEnabled(){ 

    LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE); 
    boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER) || service.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

    return enabled; 
} 

Sau khi tôi đã thực hiện chắc chắn các nhà cung cấp có sẵn thiết lập tôi một kết nối như vậy:

private void setupLocation() { 

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

    LocationListener locationListener = new LocationListener() { 
     public void onLocationChanged(final Location location) { 
      runOnUiThread(new Runnable() { 
       public void run() { 
        mLocation = location; 
        //Log.d(TAG, "Latitude: " + location.getLatitude() + " - Longitude: " + location.getLongitude()); 

        saveLocation(); 
       } 
      }); 
     } 

     public void onStatusChanged(String provider, int status, Bundle extras) {} 

     public void onProviderEnabled(String provider) {} 

     public void onProviderDisabled(String provider) {} 
    }; 

      //Can set to GPS or network, whichever is available 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 
} 

Vị trí sau đó được đặt trong biến toàn cục bất cứ khi nào nó được cập nhật và sau đó được lưu vào tùy chọn. Bằng cách này, trong trường hợp các nhà cung cấp được kích hoạt, nhưng mất một lúc để lấy lại vị trí, người dùng vẫn có thể tiếp tục sử dụng ứng dụng với vị trí đã biết cuối cùng của họ mà ứng dụng được lưu trữ (không áp dụng cho lần đầu tiên chương trình chạy).

Tôi biết tôi đã bỏ rất nhiều ở đó, nhưng tôi thấy nó không thực sự cần thiết vì nó tự giải thích hoặc đã được giải thích trong câu trả lời trước.

Chúc mừng ~

0
/* 
* getting the best location using the location manager 
* Constants.MINIMUM_TIME_BETWEEN_UPDATES = 1000 Constants.MINIMUM_TIME_BETWEEN_UPDATES  = 1 
*/ 
LocationManager mLocation; 
private String mBestProvider; 
// in your onCreate() do the following 
    mLocation = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    Criteria criteria = new Criteria(); 
mBestProvider = mLocation.getBestProvider(criteria, false); 
Location location = mLocation.getLastKnownLocation(mBestProvider); 
mLocation.requestLocationUpdates(
      LocationManager.GPS_PROVIDER, 
      Constants.MINIMUM_TIME_BETWEEN_UPDATES, 
      Constants.MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, 
      new LocationListenerManager() 
    ); 


// and use the following locationListener inner class 


private class LocationListenerManager implements LocationListener { 

    public void onLocationChanged(Location location) { 
     String message = String.format(
       "New Location \n Longitude: %1$s \n Latitude: %2$s", 
       location.getLongitude(), location.getLatitude() 
     ); 
     latitude = location.getLatitude(); 
     longitude = location.getLongitude(); 
     Toast.makeText(MapViewActivity.this, message, Toast.LENGTH_LONG).show(); 
     Log.v("poiint=====", ""+message); 

    } 

    public void onStatusChanged(String s, int i, Bundle b) { 
     Toast.makeText(MapViewActivity.this, "Provider status changed", 
       Toast.LENGTH_LONG).show(); 
    } 

    public void onProviderDisabled(String s) { 
     Toast.makeText(MapViewActivity.this, 
       "Provider disabled by the user. GPS turned off", 
       Toast.LENGTH_LONG).show(); 
    } 

    public void onProviderEnabled(String s) { 
     Toast.makeText(MapViewActivity.this, 
       "Provider enabled by the user. GPS turned on", 
       Toast.LENGTH_LONG).show(); 
    } 

} 
0

Bạn cần phải khởi tạo locationlistener trong onstartActivity trước về tạo sao cho nó vị trí có được giá trị vị trí trước khi onCreate.

Các vấn đề liên quan